Common awk Commands

Source: Internet
Author: User

Http://man.linuxde.net/awk Linux Command Daquan

WK usage: awk ' pattern {action} '

Variable name meaning
ARGC Command Line arguments
argv command-line array of elements
FileName current Input file name
FNR the record number in the current file
FS input domain delimiter, default is a space
RS Input Record delimiter
NF number of fields in the current record
NR Record Count so far
OFS output Field delimiter
ORS output Record delimiter

1. The awk '/101/' file displays a matching line containing 101 of the files.
awk '/101/,/105/' file
awk ' $ = = 5 ' file
awk ' $ = = ' CT ' file note must be enclosed with double quotes
Awk ' $ * $ >100 ' file
awk ' $ >5 && $2<=15 ' file


2. awk ' {print nr,nf,$1, $NF,} ' file displays the current record number, number of fields, and the first and last fields of each row of the files.
awk '/101/{print $1,$2 + ten} ' file displays the first to second field plus 10 of the matching line of the files.
awk '/101/{print $1$2} ' file
awk '/101/{print $ A} ' file displays the first to second field of a matching row of file files, but there is no delimiter in the middle of the display.


3, DF | awk ' $4>1000000 ' obtains input through pipe breaks, such as: Displays rows where the 4th field satisfies a condition.


4, awk -f  "|"   ' {print $1} '    file  according to the new delimiter ' | ' to operate.  
   awk   ' begin { fs= "[:  \t|]"  } 
   {print $1,$2,$3} '       file  By setting the input delimiter (fs= "[:  \t|]" ) to modify the input delimiter.  

   sep= "|" &NBSP
   awk -F  $Sep   ' {print $1} '   file  as a delimiter by the value of the environment variable SEP.     
   awk -F  ' [ :\t|] '   ' {print $1} '  file  is delimited by the value of a regular expression, which represents a space,:, TAB, | And as a delimiter. &NBSP
   awk -F  ' [] '      ' {print $1} '  file  The value of the regular expression as a delimiter, which represents [,]


5, awk-f awkfile file through the contents of awkfile files in turn control.
Cat Awkfile
/101/{print "\047 hello! \047 "}--print ' hello! after encountering a matching line '. \047 represents single quotation marks.
{Print $1,$2}-Prints the first two fields of each row because there is no mode control.


6. awk ' $ ~/101/{print $} ' file displays the row (record) of the first field in the document that matches 101.


7. awk ' BEGIN {ofs= '% '}
{print $1,$2} ' file modifies the output format by setting the output delimiter (ofs= "%").


8. awk ' begin {max=100;p rint "max=" Max} begin represents the operation that is performed before any rows are processed.
{max= ($ >max $1:max); print $, "Now Max is" max "file gets the maximum value of the first domain of the files.
(expression 1-expression 2: expression 3 equals:
if (expression 1)
Expression 2
Else
Expression 3
awk ' {print ($1>4? "High" $: "Low" $)} ' file


9. awk ' $ * $ >100 {print $} ' file displays the first field in the file that matches 101 of the row (record).


10. awk ' {$ = = ' Chi ' {$ = ' China '; print} ' file replaces the 3rd field and then displays the row (record) after it finds a matching row.
awk ' {$7%= 3; print $7} ' file will divide the 7th field by 3 and assign the remainder to the 7th domain and reprint.


11. awk '/tom/{wage=$2+$3; printf wage} ' file finds a matching row and assigns a value to the variable wage and prints the variable.


12. awk '/tom/{count++;}
End {print "Tom was found" count "Times"} ' file end means that processing is done after all input lines have been processed.


13. awk ' Gsub (/\$/, ""), Gsub (/,/, ""); cost+=$4;
END {print "The total is $" cost> "filename"} ' file Gsub function replaces $ with an empty string and outputs the result to filename.
1 2 3 $1,200.00
1 2 3 $2,300.00
1 2 3 $4,000.00

awk ' {gsub (/\$/, ""); Gsub (/,/, "");
if ($4>1000&&$4<2000) c1+=$4;
else if ($4>2000&&$4<3000) c2+=$4;
else if ($4>3000&&$4<4000) c3+=$4;
else c4+=$4; }
END {printf "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n", c1,c2,c3,c4} "' File
Completion of conditional statements by if and else if

awk ' {gsub (/\$/, ""); Gsub (/,/, "");
if ($4>3000&&$4<4000) exit;
else c4+=$4; }
END {printf "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n", c1,c2,c3,c4} "' File
Exits on a condition by exit, but the end operation is still performed.
awk ' {gsub (/\$/, ""); Gsub (/,/, "");
if ($4>3000) next;
else c4+=$4; }
END {printf "c4=[%d]\n", C4} "' File
The row is skipped on a condition by next, and the action is performed on the next line.


14, awk ' {print filename,$0} ' file1 file2 file3>fileall file1, file2, file3 file contents are written in Fileall, in the format of
Prints the file and the predecessor file name.


15. awk ' $1!=previous {close (previous); previous=$1}
{Print substr ($0,index, "" ") + 1) >$1} ' Fileall re-split the merged file into 3 files. and consistent with the original document.


16. awk ' BEGIN {"date" |getline D; Print d} ' sends the result of date execution to Getline by pipeline, assigns the variable d, and prints.


17. awk ' BEGIN {System ("echo \" Input your Name:\\c\ "") getline D;print "\nyour name is", D, "\b!\n"} '
Enter name through the getline command and display it.
awk ' BEGIN {fs= ': "; while (getline< "/etc/passwd" >0) {if ($1~ "050[0-9]_") print "}} '
Print the user name in the/etc/passwd file that contains the 050x_ user name.

18. awk ' {i=1;while (I&LT;NF) {print NF, $i; i++}} ' file implements looping through a while statement.
awk ' {for (i=1;i<nf;i++) {print NF, $i}} ' file implements loops through a for statement.
Type file|awk-f "/" '
{for (i=1;i<nf;i++)
{if (i==nf-1) {printf "%s", $i}
else {printf "%s/", $i}} ' shows the full path of a file.
Display dates with for and if
awk ' BEGIN {
for (j=1;j<=12;j++)
{flag=0;
printf "\n%d month \ n", J;
for (i=1;i<=31;i++)
{
if (j==2&&i>28) flag=1;
if (j==4| | j==6| | j==9| | j==11) &&i>30) flag=1;
if (flag==0) {printf "%02d%02d", j,i}
}
}
}‘


19. Calling system variables in awk must be in single quotation marks, or in double quotes, to indicate a string
Flag=abcd
awk ' {print ' $Flag '} ' result is ABCD
awk ' {print ' $Flag '} ' result is $flag

The above transfer from Chinaunix, the following is their own summary:

Sum:

$awk ' begin{total=0}{total+=$4}end{print total} ' a.txt-----sum the fourth field of the A.txt file!

    • $ Awk '/^ (no|so)/' Test-----Prints all lines that begin with mode no or so.

    • $ Awk '/^[ns]/{print $ ' test-----Print this record if the record starts with N or S.

    • $ Awk ' $ ~/[0-9][0-9]$/(print $) test-----If the first field prints this record at the end of two digits.

    • $ Awk ' $ = = 100 | | $ < ' test-----if the first or equal 100 or the second field is less than 50, the line is printed.

    • $ Awk ' = Ten ' test-----print the first field if it is not equal to 10.

    • $ Awk '/test/{print $10} ' test-----If the record contains a regular expression test, the first field is added to and printed out.

    • $ Awk ' {print ($ > 5? "OK" $: "Error" ($)} ' test-----Print the expression value after the question mark if the first field is greater than 5, otherwise the expression value after the colon is printed.

    • The $ awk '/^root/,/^mysql/' test----prints all records in the range of records that begin with the regular expression root with a record that begins with the regular expressions MySQL. If a record of the beginning of a new regular expression root is found, continue printing until the next record begins with the regular expression MySQL, or to the end of the file.

Common awk Commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.