Thursday, December 11, 2008

Wrap around the text with double quote using sed

Purpose: To convert a resource file from excel to be readable by Symbian resource compiler.
Tools: sed
Input: file exported from excel
Output: file with double quotes wraped around the 2nd column text

Format of the file exported from excel with 2 columns separated by tab may look like this:

....
///////////////////////////////////

//Description of this group xxxxxxxxxxxxx
///////////////////////////////////
// String : Message group
// Max length: 256

#define qtn_app_update_message \nUpdateMessage
#define qtn_app_cancel_message Cancel Message
////////////////

...


Here, I would like to wrap the 2nd column with double quote just for the texts which start with #define, but do not insert the quote otherwise.

$ sed -e '/^#/ s/\t/&"/g' -e '/^#/ s/$/"&/' input > output

We try with the combination of two substitions. The first is to insert the double quote after the tab found, the later is to insert
double quote before the line ending. Both substitions are executed only when the lines started with hash.