Wednesday, October 14, 2009

awk and vi trick

Global search and replace with regular expressions is one of the things that keeps me using vi. (Tags and cscope are the other two features I really like.

Here's a variant of that task that uses awk.

The intial text was:

Men     Formal
Men     Casual
and the goal text was:
        TestDialog(1, "Men", "Formal"),
        TestDialog(2, "Men", "Casual"),
The vi command to do this is (take out new lines, blogger keeps wrapping my pre text).
:.,.+1!awk -F "\t" '{
printf "\tTestDialog(
\%d, 
\"\%s\", 
\"\%s\"
),\n", 
NR, $1, $2}'

Getting the escaping right is a bit awkward, but the benefit is you have no worries about typos, and it applies to 18 rows of data just as easly as two rows.

No comments:

Post a Comment