编程知识 cdmana.com

One Linux command per day (4): awk

 picture

recommend : One a day Linux command (3):sed

Command profile

Awk  pattern scanning and processing language, Processing text and data .

awk Is a programming language , Used in linux/unix Processing of text and data . Data can come from standard input (stdin)、 One or more files , Or other command output . It is used on the command line , But more as a script .awk There are many built-in functions , For example, array 、 Functions, etc , This is it and C Language similarities , Flexibility is awk The biggest advantage .

Grammar format

awk [options] 'scripts' var=value filename

Common parameters

-F  Specify the separator ( Can be a string or regular expression )
-f  Read from script file awk command 
-v var=value  Assignment variable , Pass external variables to awk

Script basic structure

awk 'BEGIN{ print "start" } pattern{ commands } END{ print "end" }' filename

One awk Scripts are usually composed of BEGIN sentence + Pattern matching +END The sentence consists of three parts , These three parts are optional .

working principle :

  • Step 1 BEGIN sentence
  • The second step is to read a line from a file or standard input , And then execute pattern sentence , Scan files line by line until all files are read
  • Step 3 END sentence

Example show :

echo "hello " | awk 'BEGIN{ print "welcome" } END{ print "2017-08-08" }'
welcome
2017-08-08
echo -e "hello" | awk 'BEGIN{ print "welcome" } {print} END{ print "2017-08-08" }'
welcome
hello
2017-08-08
# No addition print Parameter, only the current line will be printed by default 
echo|awk '{ a="hello"; b="nihao"; c="mingongge"; print a,b,c; }'
hello nihao mingongge
# Use print When separated by commas , Printing is defined by spaces 
echo|awk '{ a="mgg"; b="mingg"; c="mingongge"; print a" is "b" or "c; }'
mgg is mingg or mingongge
#awk Of print Double quotation marks in a sentence are actually a splicing function 

Awk The variable of

Built-in variables

$0   # Current record 
$1~$n # The current record number N A field 
FS   # Enter field separator (-F The same effect ) Default space 
RS   # Enter the record separator , Default newline 
NF   # The number of fields is the column  
NR   # Record number , It's the line number , The default from the 1 Start 
OFS  # Output field separator , Default space 
ORS  # Output record separator , Default newline  

External variables

[mingongge@ ~]#a=100
[mingongge@ ~]#b=100
[mingongge@ ~]#echo |awk '{print v1*v2 }' v1=$a v2=$b
10000

Awk Operation and judgment

Arithmetic operator

+ -  Addition and subtraction 
* / &  ride   except   Seeking remainder 
^ *   Exponentiation 
++ --  Increase or decrease , As a prefix or suffix 
[mingongge@ ~]#awk 'BEGIN{a="b";print a,a++,a--,++a;}'
b 0 1 1
[mingongge@ ~]#awk 'BEGIN{a="0";print a,a++,a--,++a;}'
0 0 1 1
[mingongge@ ~]#awk 'BEGIN{a="0";print a,a++,--a,++a;}'
0 0 0 1
# Like any other programming language , All operations are performed as arithmetic operators , The operands are automatically converted to values , All non numerical values become 0

Assignment operator

= += -= *= /= %= ^= **=

Regular operators

~ !~   Match regular expression / Does not match regular expression 

Logical operators

||  &&   Logic or    Logic and 

Relational operator

< <= > >= != = 

Other operators

$    Field reference  
 Space   String linker 
?:    Ternary operator 
ln    Whether there is a key value in the array 

Awk Regular

^     Line header locator 
$     End of line locator 
.     Match any single character 
*     matching 0 One or more leading characters ( Including return )
+     matching 1 One or more leading characters 
?     matching 0 Or 1 Leading characters  
[]    Matches any character in the specified character group /^[ab]
[^]   Matches any character that is not in the specified character group 
()    subexpression 
|     perhaps 
     Escape character 
~,!~  Matched or unmatched conditional statements 
x{m} x The characters repeat m Time 
x{m,} x The characters repeat at least m Time 
X{m,n} x The characters repeat at least m But I can't afford it n Time ( Parameters need to be specified -posix or --re-interval)

Awk introduces

awk –F : ‘{print $2}’ datafile
# With : Separate the second column 
awk –F : ‘/^Dan/{print $2}’ datafile
# With : Separate print to Dan The second column at the beginning of the line 
awk –F : ‘/^[CE]/{print $1}’ datafile 
# Print to C or E The first column of the opening line 
awk –F : ‘{if(length($1) == 4) print $1}’ datafile 
# Print to : Separated and of length 4 The first column of characters 
awk –F : ‘/[916]/{print $1}’ datafile
# matching 916 What's the best way to do it : Separate print the first column 
awk -F : '/^Vinh/{print "a"$5}' 2.txt
# Display with Dan Start the line and add... Before the fifth column a
awk –F : ‘{print $2”,”$1}’  datafile
# Print the second column, the first column and use , Separate 
awk -F : '($5 == 68900) {print $1}' 2.txt
# With : Separate print the fifth column is 68900 The first column  
awk -F : '{if(length($1) == 11) print $1}' 2.txt
# Print to : Separated and of length 4 The first column of characters 
awk -F : '$1~/Tommy Savage/ {print $5}' 2.txt
awk -F : '($1 == "Tommy Savage") {print $5}' 2.txt
# Print to : Separate and the first column is Tommy Savage The fifth column of 
ll |awk 'BEGIN {size=0;} {size=size+$5;} END{print "[end]size is ",size}'
# Count the number of bytes of files in the directory 
awk 'BEGIN{size=0;} {size=size+$5;} END{print "[end]size is ",size/1024/1024,"M"}' 
# With M Displays the number of bytes in the directory in units 
awk 'BEGIN{a=10;a+=10;print a}'
20 
#a+10 Equivalent to  a=a+10
echo|awk 'BEGIN{a="100testaaa"}a~/test/{print "ok"}' 
# Regular matching a  Is there a test character , Set up printing ok
awk 'BEGIN{a="b";print a=="b"?"ok":"err"}'
ok
awk 'BEGIN{a="b";print a=="c"?"ok":"err"}'
err
# Ternary operator ?:
awk '/root/{print $0}' passwd 
# Match all contains root The line of 
awk -F: '$5~/root/{print $0}' passwd 
#  Use semicolons as separators , Matching first 5 The fields are root The line of 
ifconfig eth0|awk 'BEGIN{FS="[[:space:]:]+"} NR==2{print $4}'
# Print IP Address 
awk '{print toupper($0)}' test.txt
#toupper yes awk Built in functions , Convert all lowercase letters to uppercase 

One a day Linux command (1):xargs

One a day Linux command (2):od

image

版权声明
本文为[Migrant worker brother]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224120110860H.html

Scroll to Top