The basic format:
awk 'BEGIN { action; }
/search/ { action; }
END { action; }' input_file
awk '/search_pattern/ { action_to_take_on_matches; another_action; }' file_to_parse
awk '/^UUID/ {print $1;}' /etc/fstab
Awk Internal Variables and Expanded Format
FILENAME: References the current input file.
FNR: References the number of the current record relative to the current input file. For instance, if you have two input files, this would tell you the record number of each file instead of as a total.
FS: The current field separator used to denote each field in a record. By default, this is set to whitespace.
NF: The number of fields in the current record.
NR: The number of the current record.
OFS: The field separator for the outputted data. By default, this is set to whitespace.
ORS: The record separator for the outputted data. By default, this is a newline character.
RS: The record separator used to distinguish separate records in the input file. By default, this is a newline character.
we can change some of the internal variables in the BEGIN section.
sudo awk 'BEGIN { FS=":"; }
{ print $1; }' /etc/passwd
awk '$2 ~ /^sa/' favorite_food.txt
awk '$2 !~ /^sa/' favorite_food.txt
awk '$2 !~ /^sa/ && $1 < 5' favorite_food.txt
Read full article from How To Use the AWK language to Manipulate Text in Linux | DigitalOcean
awk 'BEGIN { action; }
/search/ { action; }
END { action; }' input_file
awk '/search_pattern/ { action_to_take_on_matches; another_action; }' file_to_parse
awk '/^UUID/ {print $1;}' /etc/fstab
Awk Internal Variables and Expanded Format
FILENAME: References the current input file.
FNR: References the number of the current record relative to the current input file. For instance, if you have two input files, this would tell you the record number of each file instead of as a total.
FS: The current field separator used to denote each field in a record. By default, this is set to whitespace.
NF: The number of fields in the current record.
NR: The number of the current record.
OFS: The field separator for the outputted data. By default, this is set to whitespace.
ORS: The record separator for the outputted data. By default, this is a newline character.
RS: The record separator used to distinguish separate records in the input file. By default, this is a newline character.
we can change some of the internal variables in the BEGIN section.
sudo awk 'BEGIN { FS=":"; }
{ print $1; }' /etc/passwd
awk '$2 ~ /^sa/' favorite_food.txt
awk '$2 !~ /^sa/' favorite_food.txt
awk '$2 !~ /^sa/ && $1 < 5' favorite_food.txt
Read full article from How To Use the AWK language to Manipulate Text in Linux | DigitalOcean
No comments:
Post a Comment