User Tools

Site Tools


awk:awk_field_separator_delimiters

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:

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:

awk '{OFS="-"}{print $2,$4}' test.txt

returns:

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.txt · Last modified: 2022/06/13 10:41 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki