The Begin and End keywords are special patterns that are used to execute commands before or after the data stream is read
1. Built-in variables
FieldWidths: A column of numbers separated by spaces that defines the exact width of each data field
FS: Input Field delimiter
RS: input data row delimiter
OFS: Output field delimiter
ORS: Output data row delimiter
[email protected] conf]# cat Data2data1,data2,data3,data4,data5data6,data7,data8,data9,data10data11,data12,data13 , DATA14,DATA15
[[email protected] conf]# gawk ' begin{fs= ","; ofs= "-"} {print $1,$2,$3} ' Data2data1-data2-data3data6-data7-data8data11-data12-data13
Data flow in multiple rows
Set the RS variable to a blank string and leave a blank line in the data row. Gawk will treat each blank line as a data row delimiter
[email protected] conf]# cat data3tom Mullen123 Main streetchicago.il 6001 (312) 555-1234frank Williams456 Oak Streetindia Npolis. In 46201 (317) 55-9786
[Email protected] conf]# gawk ' begin{fs= "\ n"; Rs= ""} {print $1,$4} ' Data3tom Mullen (312) 555-1234frank Williams (317) 55-9786
2. Data variables
FNR: Number of data rows in the current data file
NF: Total number of fields in the data file
NR: Number of input data rows processed
[[email protected] conf]# gawk ' begin{fs= ":"; ofs= ";"} {print $, $NF} '/etc/passwdroot;/bin/bashbin;/sbin/nologindaemon;/sbin/nologinadm;/sbin/nologinlp;/sbin/nologin
[email protected] conf]# cat Data2data1,data2,data3,data4,data5data6,data7,data8,data9,data10data11,data12,data13 , Data14,data15[[email protected] conf]# gawk ' begin{fs= ', "}{print $," fnr= "FNR," nr= "Nr}end{print" There were ", NR," Records processed "} ' data2 data2data1 fnr=1 nr=1data6 fnr=2 nr=2data11 fnr=3 nr=3data1 fnr=1 nr=4data6 FNR=2 NR=5data11 FN R=3 Nr=6there were 6 records processed
3. Custom variables
Variable names cannot start with a number,
[[email protected] conf]# gawk ' > Begin{> testing= "This was a test" > Print testing>} ' This is a test
[Email protected] conf]# gawk ' begin{x=4; X=x*2+3;print X} ' 11
Assigning a value to a variable on the command line
[email protected] tmp]# cat script1#!/bin/bash#begin{fs= ","}{print $n}[[email protected] tmp]# cat Data3data1,data2, Data3,data4,data5data6,data7,data8,data9,data10data11,data12,data13,data14,data15[[email protected] tmp]# gawk-f SCRIPT1 n=2 Data3data2data7data12
After setting the variable, this value is not available in the Beginu section of the Code
[Email protected] tmp]# Vim Script1#!/bin/bash#begin{print "The starting value is", N; Fs= ","}{print $n}
[Email protected] tmp]# gawk-f script1 n=3 data3the starting value is Data3data8data13
Plus-V, allows you to specify the variables set before the Begin Code section
[Email protected] tmp]# gawk-f script1-v n=3 data3the starting value is 3data3data8data13
4. Iterating through an array
for (var in array)
{
Statements
}
The FOR statement executes the statements once each time the next index value of the associative array array is assigned to the variable var.
Remember that this variable is an index value and not a value of an array element
[[email protected] tmp]# gawk ' begin{var["a"]=1var["G"]=2var["M"]=3var["U"]=4for (Test in Var) {print "Index:", Test, "- Valuse: ", Var[test]}} ' Index:u-valuse:4index:m-valuse:3index:a-Valuse:1index:g-Valuse:2
To delete an array of variables
To remove an array index from an associative array use a special command
Delete Array[index]
[[email protected] tmp]# gawk ' begin{var["a"]=1var["G"]=2for (test in Var) {print "Index:", Test, "-Value", Var[test]}dele Te var["G"]print "------------" for (test in Var) {print "Index:", Test, "-Value",var[test]}>} ' index:a-value 1Index : G-value 2------------index:a-Value 1
5. Matching operator
The match operator (~), which allows a regular expression to be qualified in a specific data field in the data row.
$ ~/expression/
Match the second data segment with the regular expression/^data2/to filter the second field to the line that begins with the text data2
[email protected] tmp]# cat Data3data1,data2,data3,data4,data5data6,data7,data8,data9,data10data11,data12,data13, Data14,data15data21,data22,data23,data24,data25[[email protected] tmp]# gawk ' begin{fs= ', '} $ ~/[^data2]/{print} ' Data3data6,data7,data8,data9,data10data11,data12,data13,data14,data15
Gawk program scripts are often powerful tools for finding specific data elements in a data file
[Email protected] tmp]# gawk-f: ' ~/root/{print $, $NF} '/etc/passwdroot/bin/bash
Use the! symbol to exclude the matching of regular expressions
$!~/expression/
Gawk-f: ' $!~/root/{print $, $NF} '/etc/passwd
6. Mathematical expressions
x = = y
X <= y
X < y
X >= y
X > Y
Displays all system users that belong to the root user group (group ID 0)
[Email protected] tmp]# gawk-f: ' $4==0{print $ '/etc/passwdrootsyncshutdownhaltoperator
Use expressions for text data, you must be careful, unlike regular expressions, expressions must match exactly
[Email protected] tmp]# gawk-f, ' $1== ' data1 ' {print $} ' data3data1
6. If statement
if (condition)
Statement1
or if (condition) statement1
[[email protected] tmp]# cat Data410531355[[email protected] tmp]# gawk ' {if ($1>20) print '} ' data455
[[email protected] tmp]# gawk ' {> if ($1>20) > {> x=$1 * 2> print x>}>} ' data4110
The IF statement of the Gawk also supports the ELSE clause, allowing one or more statements to be executed if the condition of the IF statement is not valid.
[[email protected] tmp]# gawk ' {> if ($1>100) > {> x=$1 * 2> print x>} else> {> x=$1/2> Print x>}} ' data452.51.56.527.5
You can write it like that.
if (condition) Statement1:else Statement2
[[email protected] tmp]# gawk ' {if ($1>100) Print $1*2;else print $1/2} ' data452.51.56.527.5
7. While statement
while (condition)
{
Statement1
}
[[email protected] tmp]# cat data5120 135160 113 140145 (215[[email protected] tmp]# gawk ' {total=0i=1while (i<4 {total+= $i i++}avg=total/3print "Averaget:", avg} ' Data5averaget:128.333averaget:137.667averaget:176.667averaget: 0
8. Do-while Statement
Do
{
Statement1
} while (condition)
[[email protected] tmp]# gawk ' {total=0i=1do{total+= $i i++}while (total<150) print Total} ' data5250160315
9. For statement
For (variable assignment;condition;interation process)
[[email protected] tmp]# gawk ' {> tatal=0> for (i=1;i<4;i++) > {> total+= $i >}> avg=total/3> pri NT "Average:",avg>} ' data5average:128.333average:266average:442.667average:442.667[[email protected] tmp]# gawk ' {for (i=1;i<4;i++) total+= $i; avg=total/3;print "Average:", avg} ' Data5average:128.333average:266average: 442.667average:442.667[[email protected] tmp]# cat data5120 130 135160 113 140145 170 215
10. Format printing
650) this.width=650; "title=" 11.jpg "alt=" wkiol1x72efi0jofaae51wn1sga949.jpg "src=" http://s3.51cto.com/wyfs02/M01/ 73/62/wkiol1x72efi0jofaae51wn1sga949.jpg "/>
Manually add line breaks at the end of the printf command to generate a new row
If you need several separate printf commands to print multiple outputs on the same line
[email protected] tmp]# cat Data3data1,data2,data3,data4,data5data6,data7,data8,data9,data10data11,data12,data13, Data14,data15data21,data22,data23,data24,data25[[email protected] tmp]# gawk ' begin{fs= ","}{printf "%s", $1}END{ printf "\ n"} ' data3data1 data6 data11 data21
[[email protected] tmp]# gawk ' begin{fs= ","}{printf "%s", $} ' data3data1 data6 data11 data21 [[email protected] tmp]#
This article is from the "Scattered People" blog, please be sure to keep this source http://zouqingyun.blog.51cto.com/782246/1695991
Gawk Advanced Use