sed & awk 101 hacks study notes -awk

Awk is a maintenance and processing of text data files, powerful programming language.
When the text data of a certain format, i.e., each row of data contains a plurality of fields to boundary-delimited especially useful.

Awk basic grammar:
Awk -Fs '} {Command' INPUT-File
-F as field delimiters. If not specified, the default will use the space as a delimiter

A typical awk program comprises the following three regions:

  1. BEGIN region
    syntax Begin region: BEGIN {awk-commands} BEGIN command areas initially only performed once before awk command execution body region.  BEGIN area very suitable for printing packet header information, and is used to initialize variables.  BEGIN area may have one or more awk command   capital must use the keyword BEGIN BEGIN field is optional
  2. body region
    syntax body area: / pattern / {action} body region read each command line from the input file will be executed if the input file has a  row 10, that will execute the command body region 10 (one per line executed once)  Body region is not represented only by a regular mode and command by any keyword.
  3. END Block
    Syntax END region: END {awk-commands} END region awk After all operations performed, and is performed only once.  END region is very suitable for newspaper printing the end of file information, as well as to do some cleanup actions  END region may have one or more awk command   keyword END must use uppercase END field is optional

When it encounters a file containing a plurality of field delimiter, do not worry, FS can handle. You can use regular expressions to specify multiple field separator, such as FS = "[,:%] " designated field delimiter can be a comma or semicolon: Percent or%.
awk 'BEGIN {FS = "[ ,:%]"} {print $ 2, $ 3}' employee-multiple-fs.txt

OFS - Output Field Separator

awk -F ‘,’ ‘BEGIN {OFS=":"} {print $2,$3}’ employee.txt

Also, please note that the print statement manipulation

Guess you like

Origin blog.csdn.net/oTobias/article/details/102542273
awk