awk
AWK is an excellent text-processing tool, one of the most powerful data-processing engines available in Linux and Unix environments. AWK offers extremely powerful features: regular-expression matching, pattern loading, flow control, math operators, process-control statements, and even built-in variables and functions. It has nearly every elegant feature a complete language should have. Put most simply, AWK is a programming-language tool for processing text.
Syntax
An AWK program is made up of a series of pattern-action pairs
pattern { action }
pattern表示AWK在数据中查找的内容,而action是在找到匹配内容时所执行的一系列命令。
{print} outputs the content of the current record. In AWK, a record is split into “fields”, which can be displayed or used individually
{print $1} shows the 1st field of the current record
{print $1, $3} shows the 1st and 3rd fields of the current record, separated by the predefined Output Field Separator (OFS), whose default value is a single space
{print $0} shows the entire current record
{print $NF} shows the last field
{print $(NF-1)} shows the second-to-last field
{sum+=$NF} END {print sum} computes a total
-F specify your delimiter
Built-in Variables
AWK’s built-in variables include field variables, such as $1, $2, $3, and $0. These variables hold the content of the fields in a record. Built-in variables also include several other variables:
NR: the number of records read so far.
NF: the number of fields in the current record. The last field of a record can be referenced as $NF.
FILENAME: the name of the current input file.
FS: the “field separator”, used to split an input record into fields. Its default value is “whitespace”, i.e. spaces and tabs. FS can be replaced with other characters to change the field separator.
RS: the current “record separator”. By default, each line of input is treated as one record, so the default record separator is the newline character.
OFS: the “output field separator”, the symbol that separates the arguments of a print command. Its default value is a space.
ORS: the “output record separator”, the symbol placed between each print command’s output. Its default value is the newline character.
OFMT: the “format for numeric output”, whose default value is “%.6g”.
Example
awk -F '"' 'NR==1,NR==2 BEGIN{ORS=""}{print $2}'
Specifies “ as the delimiter, sets the range to the first through second record, replaces the record separator’s value with null, and prints the second field