Awk usage: awk 'pattern' {action }'

Source: Internet
Author: User

Reproduced from: http://bbs.chinaunix.net/viewthread.php? Tid = 691456

Awk usage: awk 'pattern' {action }'

Awk Environment Variables

Variable Description
$ N The Nth field of the current record. The fields are separated by FS.
$0 Complete input records.
ARGC The number of command line parameters.
ARGIND Location of the current file in the command line (starting from 0 ).
ARGV Array containing command line parameters.
CONVFMT Number conversion format (default value: %. 6g)
ENVIRON Environment Variable join array.
ERRNO Description of the last system error.
FIELDWIDTHS Field width list (separated by Space key ).
FILENAME The current file name.
FNR Same as NR, but relative to the current file.
FS Field separator (any space by default ).
IGNORECASE If it is true, case-insensitive matching is performed.
NF The number of fields in the current record.
NR Number of current records.
OFMT The output format of the number (default value: %. 6g ).
OFS Delimiter of the output field (the default value is a space ).
ORS The output record delimiter (the default value is a line break ).
RLENGTH The length of the string matched by the match function.
RS Record separator (a line break by default ).
RSTART The first position of the string matched by the match function.
SUBSEP Array subscript separator (default value: \ 034 ).

1. awk '/101/'file: The file contains 101 matching rows.
Awk '/101/,/105/' file
Awk '$1 = 5' file
Awk '$1 = "CT"' file must contain double quotation marks
Awk '$1 * $2> 100' file
Awk '$2> 5 & $2 <= 15' file
2. awk '{print NR, NF, $1, $ NF,}' file displays the current file record number, number of fields, and the first and last fields of each row.
Awk '/101/{print $1, $2 + 10}' file displays the first and second fields of the matching row of the file plus 10.
Awk'/101/{print $1 $2} 'file
Awk '/101/{print $1 $2}' file displays the first and second fields of the matching row of the file, but it shows that there is no Separator in the middle of the time domain.
3. df | awk '$4> 1000000' is input through a pipeline operator. For example, a row with 4th fields meeting the conditions is displayed.
4. awk-F "|" '{print $1}' file is operated according to the new separator "|.
Awk 'in in {FS = "[: \ t |]"}
{Print $1, $2, $3} 'file modifies the input Separator by setting the input separator (FS = "[: \ t |.

Sep = "|"
Awk-F $ Sep '{print $1}' file uses the environment variable Sep value as the separator.
Awk-F' [: \ t |] ''{print $1} 'file uses the value of the regular expression as the separator. Here, space,:, TAB, and | are used as the separator at the same time.
Awk-F' [] [] ''{print $1} 'file uses the value of the regular expression as the separator, which indicates [,]
5. The awk-f awkfile file is sequentially controlled by the awkfile content.
Cat awkfile
/101/{print "\ 047 Hello! \ 047 "} -- print 'Hello! '. \ 047 represents single quotes.
{Print $1, $2} -- because there is no mode control, print the first two fields of each row.
6. awk '$1 ~ /101/{print $1} 'file: the first field in the file matches 101 rows (records ).
7. awk 'in in {OFS = "% "}
{Print $1, $2} 'file modifies the output format by setting the output separator (OFS = "%.
8. awk 'in in {max = 100; print "max =" max} BEGIN indicates the operation performed before any row is processed.
{Max = ($1> max? $1: max); print $1, "Now max is" max} 'file gets the maximum value of the first domain of the file.
(Expression 1? Expression 2: expression 3 is equivalent:
If (expression 1)
Expression 2
Else
Expression 3
Awk '{print ($1> 4? "High" $1: "low" $1)} 'file
9. awk '$1 * $2> 100 {print $1}' file indicates that the first domain in the file matches 101 rows (records ).
10. awk '{$1 = 'chi' {$3 = 'China '; print} 'file: Find the matching row, replace the first 3rd fields, and then display the row (record ).
Awk '{$ 7% = 3; print $7}' file divides the 7th domain by 3, assigns the remainder to the 7th domain, and then prints it.
11. awk '/tom/{wage = $2 + $3; printf wage}' file: Find the matching row, assign a value to the variable wage, and print the variable.
12. awk '/tom/{count ++ ;}
END {print "tom was found" count "times"} 'file END indicates processing after all input rows are processed.
13. awk 'gsub (/\ $/, ""); gsub (/,/, ""); cost + = $4;
END {print "The total is $" cost> "filename"} 'file gsub function replaces $ and with an empty string, and then 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
Use if and else if to complete the Condition Statement

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
Exit is used to exit when a condition is specified, but the END operation is still executed.
Awk '{gsub (/\ $/, ""); gsub (/,/,"");
If ($4 & gt; 3000) next;
Else c4 + = $4 ;}
END {printf "c4 = [% d] \ n", c4} "'file
Use next to skip this row in case of a condition and perform operations on the next row.

14. awk '{print FILENAME, $0}' file1 file2 file3> fileall writes all contents of file1, file2, and file3 to fileall. The format is
Print the file and the file name.
15. awk '$1! = Previous {close (previous); previous = $1}
{Print substr ($0, index ($0, "") + 1)> $1} 'fileall splits the merged file into three files. And is consistent with the original file.
16. awk 'in in {"date" | getline d; print d} 'sends the execution result of date to getline through the pipeline, assigns it to the variable d, and then prints it.
17. awk 'in in {system ("echo \" Input your name: \ c \ ""); getline d; print "\ nYour name is", d, "\ B! \ N "}'
Use the getline command to enter and display the name.
Awk 'in in {FS = ":"; while (getline <"/etc/passwd"> 0) {if ($1 ~ "050 [0-9] _") print $1 }}'
Print the username in the/etc/passwd file that contains the 050x _ username.

18. awk '{I = 1; while (I <NF) {print NF, $ I; I ++}' file loops through the while statement.
Awk '{for (I = 1; I <NF; I ++) {print NF, $ I}' file loops through the 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 date with for and if
Awk 'in in {
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. The system variable to be called in awk must be enclosed in single quotation marks. If it is double quotation marks, it indicates a string.
Flag = abcd
Awk '{print' $ flag'} 'returns abcd
Awk '{print "$ Flag"}' returns $ Flag

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.