2012-11-10

Quick Play with Pattern and Hold Spaces in Sed

I had some rows of data that required two changes and both changes had to appear on separate lines. For example, if the input was ...

123
456
789

... and the requirement was to find '456', replace '5' with 'a' on one line and 'b' on another line so that the output would look like this:

123
4a6
4b6
789

The sed script below applies the first change, prints it, applies the second change and let sed print the pattern space at the end of the function.

/456/ {
  s/5/a/p --> pattern space = 4a6, print pattern space.
  s/a/b/  --> pattern space = 4b6
}         --> print pattern space, 4b6

In the script above, notice that the input string was first modified from '456' to '4a6' so the second statement has to replace 'a' with 'b'.

Another approach is to create two lines first (i.e. 456\n456) then replace the first '5' with 'a' and replace the second '5' with 'b'.

/456/ {
  h      --> hold space = pattern space, 456
  H      --> hold space = 456\n456 (456 + \n + pattern space)
  g      --> pattern space = hold space, 456\n456
  s/5/a/ --> pattern space = 4a6\n456
  s/5/b/ --> pattern space = 4a6\n4b6
}        --> print pattern space, '4a6\n4b6'

The script above uses sed's hold space to construct the two lines first. I don't know if the second script is clearer than the first but it was a simple problem that I could use to play with sed's pattern and hold spaces.

2012-11-03

Giving Up On Windows 7 Explorer

Giving up on Windows 7 Explorer because file management is harder than WinXP Explorer since it's not possible to disable "auto row highlight" and "select full row".
Feature #1 is confusing because two items can be highlighted in a directory list when you navigate using the mouse and cursor keys: the item under the pointer gets highlighted automatically without having to activate the window but the previously selected item remains highlighted (see the two highlighted folders in the image below). It's too easy to accidentally copy or move the wrong object using keyboard shortcuts because I couldn't tell which object was the active one. In WinXP, you have to click in the window before the selection is changed and only one item is highlighted. Feature #2 means you have to find a blank space (usually the small gap between the item and the edge of the window) with the pointer to show the folder's context menu otherwise you see the highlighted item's context menu.
Playing with an alternative file manager called FreeCommander now. Out of the box, it highlights the entire row but you can disable that feature in Settings, View, File/Folder List, Full row select.