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.

2012-10-28

Australian Accounting Number Formats

Number format codes for Excel and Libre Calc used in Australian accounting (Excel does not have this number format code available as a default while Libre Calc has the second one).

Number format code123456.78-123456.780.00
#,##0_);(#,##0);-_)123,457 (123,457)
#,##0.00_);(#,##0.00);-_)123,456.78 (123,456.78)

Notes:

  • Semicolons delimit the sign of the number being formatted: positive;negative;zero.
  • The underscore before the right parenthesis: append a trailing space the size of the parenthesis so that positive numbers are right-aligned with negative numbers (which are enclosed in parentheses).
  • Using the first format code, your numbers are formatted without cents.

The underlying value in the cell hasn't changed, just the presentation. Use sparingly; you can confuse a user who tries to use what they see in the cell in their formula and wonder why their formula doesn't work.

2012-09-08

Convert DOS Lines Into One Comma-Separated Line

Note to myself: Convert multiple DOS lines into one comma-separated line using this GnuWin command: tr -s "\r\n" ,. DOS lines use two control characters \r\n to mark the end of a line. The -s option replaces multiple original characters with only one replacement character.