Awk usage: awk 'pattern' {action }'

Source: Internet
Author: User

Awk usage: awk 'pattern' {action} 'variable name meaning ARGC command line variable number ARGV command line variable element array FILENAME current input file name FNR current file record number FS input domain separator, default is a space RS input record delimiter NF number of fields in the current record NR number of records so far OFS output domain separator ORS output record separator 1 awk '/101/'file display file contains 101 matching rows. Awk '/101 /, /105/'file awk' $1 = 5 'file awk' $1 = "CT" 'file note that the 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 record number, number of fields, and the first and last fields of each row of the file. 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, however, there is no Separator in 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 by setting the input separator (FS = "[: \ t |] ") modify the input separator. 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, here, [,] 5 and awk-f awkfile are controlled sequentially through the content of the awkfile file. 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 to: 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 shows that the first field 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 uses the if and else if condition statements 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 with exit when a condition is specified, but the END operation is still executed. Awk '{gsub (/\ $/, ""); gsub (/,/, ""); if ($4> 3000) next; else c4 + = $4;} END {printf "c4 = [% d] \ n", c4} "'file uses next to skip this row when a condition is specified, perform operations on the next row. 14. awk '{print FILENAME, $0}' file1 file2 file3> fileall writes all the content of file1, file2, and file3 to fileall, in the format of a print file and prefix 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 "} 'enter the name through the getline command and display it. 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. Use for and if to display the date 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. To call system variables in awk, you must use single quotation marks, the string Flag = abcd awk '{print' $ Flag '}' is returned as abcd awk '{print "$ Flag"}' and above $ Flag

Related Article

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.