Common examples of awk commands

Source: Internet
Author: User

This article is edited from http://developer.hi.baidu.com/#/detail/65330911?simple awk '/101/'file. The file contains 101 matching rows. Awk '/101/,/105/' file: The file contains rows of 101 or 105. Awk '$1 = 5' file displays the 1st rows with 5 fields in the file. Awk '$1 = "CT "'
File: the row awk '$1 * $2> 1st' Where the 100 fields in the file are CT '$1 * $2> 1st 'file: the row whose product of the 100 fields and the second field is greater awk '$2> 5 & $2 <= 15' file: displays the rows with the Second Field greater than 5 but less than or equal to 15 in the file. 2. The field 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 field of the matching row containing 101 in the file, and the second field plus 10 is also displayed, and they are separated by commas. Awk'/101/{print $1 $2 }'
File displays the first field of the matching row containing 101 in the file, and adds 10 to the second field, but there is no delimiter between them. 3. Input DF | awk '$4> 1000000' in the MPs queue. The input is obtained by using the MPs queue operator. Rows with 4th field fields greater than 1000000 are displayed. 4. The field domain delimiter 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 delimiter variable FS = "[: \ t |] "means that colon, table, and | are used as separators. 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. 5. Load the awk script command file awk-F awkfile file to load the awkfile awk script command for operations. Cat awkfile/101/{print "\ 047 hello! \ 047 "} print 'Hello! '. \ 047 represents single quotes. 6. The first line matches awk '$1 ~ /101/{print
$1} 'file: the first field in the file matches 101 rows (records ). 7. The output delimiter awk 'in in {OFS = "%"} {print $1, $2} 'file modifies the output format by setting the output separator (OFS = "%. 8. The conditional expression 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. Here is a comparison of the characters ASC code. Expression 1? Expression 2: expression 3 is equivalent to: If (expression 1) expression 2else expression 3 can also use the conditional expression awk '{print ($1> 4? "High" $1: "low" $1 )} 'file9, mathematical operation awk' $1 * $2> 100 {print $1} 'the file displays rows 10 whose product of the 1st fields and the second field is greater than 100, change the field 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. Field combination awk '/Tom/{Wage = $2 + $3; print wage}' file: Find the matching row, assign a value to the variable wage, and print the variable. 12. Count matching records awk '/Tom/{count ++ ;} end {print "Tom was found" count "times"} 'file end indicates processing after all input rows are processed. 13. The function calls awk 'gsub (// $/, ""); gsub (/,/,"");
Cost + = $4; end {print "the total is $" cost> "FILENAME"} 'file gsub function replaces $ and comma with empty strings, and then outputs the result to filename. 14. conditional statement 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 conditional 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 exits with exit, but the end operation is still executed. Awk '{gsub (/\ $/, ""); gsub (/,/, ""); if ($4> 3000) next; else C4 + = $4;} end {printf "C4 = [% d] \ n", C4} 'file skips this row when a condition is specified through next, perform operations on the next row. 15. Operate on multiple files awk '{print filename, $0}' file1
File2 file3> fileall writes all contents of file1, file2, and file3 to fileall, and starts awk '$1 with the source file name of this row in each row! = 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. 17. getlineawk '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. Awk 'in in {system ("Echo \" input your name: \ ""); 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/etc/passwd file. The username contains the 050x _ username. Note: experiment failed 18. Calling System variables in awk must use single quotation marks. If it is double quotation marks, it indicates a string.

Flag = abcdawk '{print' $ flag '}' is returned as ABCD. Note: When the experiment fails, the awk '{print "$ flag"}' result is $ flag19, the cyclic awk '{I = 1; while (I <= NF)
{Print NF, $ I; I ++} 'file uses the while statement to implement a loop. Awk '{for (I = 1; I <= NF; I ++)
{Print NF, $ I} 'file uses the for statement to implement a loop. Type file | awk-F "/" '{for (I = 1; I <= NF; I ++) {if (I = NF) {printf "% s", $ I} else {printf "% S/", $ I }}' shows the full path of the file command. 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 }}} 'Use for and if to display the date

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.