awk:awk_field_separator_delimiters
This is an old revision of the document!
Table of Contents
AWK - AWK Field Separator Delimiters
Assuming a file exists, test.txt, with the following contents:
10 Peter Terence Roux 45 11 Virginia Genevieve Roux 45 12 Felix Devon Roux 5 13 David Bruce Stevenson 48 14 Bob James Smith 16 48 Adam Winter Ridley 23
Specifying Input Field Separator Delimiter
To set an alternative field delimiter, issue the following command:
awk '{FS=":"}{print $2,$4}' test.txt
or
awk -F: '{print $2,$4}' test.txt
NOTE: This is useful when a space is not being used as the delimiter in the input file.
- AWK FS is any single character or regular expression which you want to use as a input field separator.
- AWK FS can be changed any number of times, it retains its values until it is explicitly changed.
- If you want to change the field separator, its better to change before you read the line; so the change affects the line that is read.
Specifying Output Field Separator Delimiter
To set an alternative field delimiter for the output, issue the following command:
awk '{OFS="-"}{print $2,$4}' test.txt
which will display something like:
Peter-Roux Virginia-Roux Felix-Roux David-Stevenson Bob-Smith Adam-Ridley
NOTE: The OFS variable is used to specify the output delimiter.
awk/awk_field_separator_delimiters.1609947619.txt.gz · Last modified: 2021/01/06 15:40 by peter