field separator for awk

You can change the value of FS in the awk program with the assignment operator, `=’ (see section Assignment Expressions). Often the right time to do this is at the beginning of execution, before any input has been processed, so that the very first record will be read with the proper separator. To do this, use the special BEGIN pattern (see section BEGIN and END Special Patterns). For example, here we set the value of FS to the string “,”:

awk ‘BEGIN { FS = “,” } ; { print $2 }’


More generally, the value of FS may be a string containing any regular expression. Then each match in the record for the regular expression separates fields. For example, the assignment:

FS = “, \t”

FS can be set on the command line. You use the `-F’ argument to do so. For example:

awk -F, ‘program’ input-files

The value used for the argument to `-F’ is processed in exactly the same way as assignments to the built-in variable FS. This means that if the field separator contains special characters, they must be escaped appropriately. For example, to use a `\’ as the field separator, you would have to type:

# same as FS = “\\”
awk -F\\\\ ‘…’ files …


It's only fair to share...Tweet about this on TwitterShare on Google+Share on FacebookShare on LinkedInEmail this to someone
Twitter Auto Publish Powered By :