Friday, September 11, 2009

Using Sed to format text for Basecamp with Textile tables markup

Sometimes I have data in rows and cells in a spreadsheet I want to copy and paste into Basecamp with the tables. You can use Sed, the UNIX stream editor, to automate formatting the tabbed text into text which will appear in tables in Basecamp.

I copy and paste the data from the rows and cells in the spreadsheet spreadsheet into a text file. I'll name my text file "1".


Pseudocode:
Read the file named "1" line by line
For each line substitute each tab with " | "
Save the results in a file named "2"

The space after the first / is actually Ctrl-V Tab. If you type Ctrl-V the shell will record the meta character for the key you type. This will replace the tabs with " | " i.e. space pipe space. The g after the last / makes sed do this globally for the entire line not just for the first instance.


Pseudocode:
Read the file named "2" line by line
For each line substitute each first character with "| "
Save the results in a file named "3"


Pseudocode:
Read the file named "3" line by line
For each line substitute each last character with " |"
Save the results in a file named "4"


Super Sed:
If you use Super Sed this can be done in one command line consisting of the above operations piped one after another because Super Sed supports Perl-like editing of the file in place.

If you use a Macintosh and MacPorts you can install Super Sed with "sudo port install ssed"

No comments:

Post a Comment