Saturday, February 21, 2009

Using multiple field separators with gawk

To define multiple field separators with gawk, set the FS variable with the appropriate regex.

For example, to use comma(,), colon(:) and equal-to (=) as field separators, following can be used

cat file.txt | gawk '{FS = "[:,=]+"} {print $3" "$5}'

The above example also prints the 3rd and 5th field elements.

1 comment:

Anonymous said...

This became pretty handy. Thanks