Application of awk under linux


Quoting the article http://www.jb51.net/article/58020.htm
Detailed explanation of awk regular expressions and built-in function examples:

1. Fuzzy matching:
awk '{if($3~/97/) print $0}' data.f: print this line if the third item contains "97"
awk '{if($4!~/ufcx/) print $0}' data.f: print if the third item does not contain ufcx


2. Exact match:

awk '{if($5==66) print $0}' data.f: print if the fifth item is 66
awk '{if($5!=66)print $0}' data.f : print if the fifth item is not 66
awk '{if($1>$5) print $0}' data.f: print if the first item is greater than the fifth item


3. Case matching:

awk '{if(/[Ss]ept/) print $0}' data.f: If it matches, print a line.
awk '/[Ss]ept/ {print $2}' data.f: if it matches, then print the second field


4. Any match:

awk '{if($2 ~/^.e/) print $0}' data.f: In the second field, the second character is e, output
awk '{if($4 ~/(lps|fcx)/) print $0}' data.f: output if the fourth field contains lps or fcx


5、&&,||:


awk '{if($3 ~/1993/ && $2==”sept”) print $0}' data.f: output if both sides are true
awk '{if($3 ~/a9/ || $2==”sept”) print $0}' data.f: output if one side is true


6. Variable definition:

awk '{date=$2;price=$5; if(date ~/[Ss]ept/) print “price is ” price}' data.f: variable definition, output price if date is sept or sept.


7. Modify the value (the value of the source file remains unchanged)

awk '{BASELINE=42; if($1>BASELINE) $5=$5+100; print $0}' data.f: three-line program, separated by ";"


If the modification is a text field, add """". For example: awk '{if($2==”may”) $2=”tt”; print $0}' data.f

above shows all data, awk '{if($2==”may”) {$2=” tt"; print $0}}' data.f This only shows the modified data. Take a closer look. In fact, the syntax is the same as c, except that a {} symbol is added to the outermost.

8. Create a new domain: (the value of the source file remains unchanged)
awk ‘{if($5>$1){$8=$5-$1;print $1,$8}}' data.f:

或者awk ‘{if($5>$1){diff=$5-$1;print $1,diff}}' data.f


9. Data statistics:

awk '{(total+=$5)}END{print total}' data.f: "{(total+=$5)}" and "{print total}" represent two different code segments, if not The cumulative result of END will be output every time, and END can be understood as the sign of the code paragraph, so that only the final result is output, that is, {print total} is executed only once.

10. Statistical file size:


ls –l | awk '{if(/^[^d]/) total=+$5}END{print "total KB:" total}': /^[^d]/ The field value $1 can be omitted for the first match of the line


11. Awk built-in variables:

ARGC Number of command-line parameters

ARGV Command-line parameter arrangement

ENVIRON Support system environment variables in the queue Use

FILENAME awk to browse the file name

FNR Browse the number of records in the file

FS Set the input field separator, which is equivalent to the command line -F option

NF number of fields browsed records

NR number of records read

OFS output field separator

ORS output record separator

RS control record separator

12, awk built-in string processing function

gsub ( r, s ) is used in the entire $0 s replaces r

gsub( r, s , t ) replaces r with s throughout t

index( s , t ) returns the first position of string t in

s length( s ) returns the length of s

match( s , r ) test s Whether it contains a string matching r, return position

split ( s , a , fs ) split s into sequence a on fs

sprint ( fmt , exp ) return fmt-formatted exp in

sub ( r, s , $0) $0 s replaces the first occurrence of r

substr ( s , p ) returns the suffix part of string s starting from p

substr ( s , p , n ) returns the suffix part of string s starting from p and having length n

13. awk 'gsub(/6\./,78) {print $0}' data.f: replace all "6." with 78, and output


awk '{if($2=="Sept") {sub( /3/,"9",$0); print $0}}' data.f: replace only the first occurrence of

awk 'BEGIN{print index("hello","lo")}': the output value is 4

awk '{if($3==”3BC1997″) print length($3) ” ” $3}' data.f

awk 'BEGIN{print match("ABCD","B")}': output 2

awk 'BEGIN{print match("ABCD",/B/)}': "//" and """" have the same effect

awk 'BEGIN {print split("123#234#654", myarray, "#")}': return an array The number of elements, 123#234#654 is a string, with "#" as the delimiter, put the string into the array.

awk '{if($1==34) print substr($3,2,7)}' data.f

awk 'BEGIN{print substr("helloleeboy",2,7)}': output ellole

awk 'BEGIN{print substr ("helloleeboy",2,7)}' data.f: output n times ellole, n is the number of lines in data.f

14, awk 'BEGIN{print "May\tDay\n\nMay \104\141\171" }' :\104\141\171 means Day. \t: tab key, \n: newline, \ddd: octal

15. echo "65" | awk '{printf "%c\n",$0}': printf function, similar to c, the output is A. (ASCII code)



echo "65" | awk '{printf "%d\n",$0}': output 65 digits.

awk '{printf "%-15s %s\n",$2,$3}' data.f: "%-15s" left justified 15 characters long

awk '{if(age<$1) print $0}' age=80 data.f and awk '{age=49;if(age<$1) print $0}' The result of data.f is the same, the former passes the value into awk, and the latter defines a variable in awk.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326053793&siteId=291194637