command name:awk
Role:
Processing of text and data
Detailed Description:
Awk is a programming language that is used to process text and data under Linux/unix.
The data can come from standard input (stdin), one or more files, or the output of other commands.
It is used in the command line, but more is used as a script. Awk has many built-in features,
such as arrays, functions, and so on, which is the same as the C language, flexibility is the biggest advantage of awk.
Syntax format:
awk [options] ' scripts ' var=value filename
Common parameters:
-f Specifies the delimiter (can be a string or regular expression)
-F Read the awk command from the script file
-V Var=value assigns a user-defined variable to pass an external variable to awk
awk Script Basic structure:
awk ' begin{print ' start '} pattern{commands} end{print "END"} ' filename
An awk script is usually composed of the BEGIN statement + pattern matching +end statement, which are all optional
Working principle:
The first step executes the BEGIN statement
The second step reads a line from the file or standard input, and then executes the pattern statement, scanning the file to the file all read
The third step executes the end statement
echo "Hello World" | awk ' begin{print ' Welcome "} end{print" 2017-08-08 "} '
Welcome
2017-08-08
Echo-e "Hello World" | awk ' begin{print ' welcome '} {print} end{print "2017-08-08"} '
Welcome
Hello World
2017-08-08
#不加print参数时默认只打印当前的行
[[email protected] ~]# echo |awk ' {a= "hello"; b= "Nihao"; c= "Mingongge"; print a,b,c;} '
Hello Nihao Mingongge
#使用print以逗号分隔时, the print is delimited by a space
Echo |awk ' {a= "mgg"; b= "Mingg"; c= "Mingongge"; print a "is" B "or" C;} '
MGG is Mingg or Mingongge
#awk的print语句中双引号其实就是个拼接作用
awk variables are used:
built-in variables
$ #当前记录
$1~ $n #当前记录的第N个字段
FS #输入字段分隔符 (-f same effect) default space
RS #输入记录分割符, default line break
NF #字段个数就是列
NR #记录数, is the line number, the default starting from 1
OFS #输出字段分隔符, default space
ORS #输出记录分割符, default line break
External variable passed in
[[Email protected] ~] #a =100
[[Email protected] ~] #b =100
[[Email protected] ~] #echo |awk ' {print v1*v2} ' v1= $a v2= $b
10000
awk Operations and judgments:
Arithmetic operators
+-plus minus
*/& Multiplication Redundancy
^ * exponentiation
+ +--increase or decrease, as prefix or suffix
[[Email protected] ~] #awk ' begin{a= "B";p rint a,a++,a--, ++a;} '
B 0 1 1
[[Email protected] ~] #awk ' begin{a= "0";p rint a,a++,a--, ++a;} '
0 0 1 1
[[Email protected] ~] #awk ' begin{a= "0";p rint a,a++,--a,++a;} '
0 0 0 1
#和其它编程语言一样, all operations are performed as arithmetic operators, the operands are automatically converted to numeric values, and all non-numeric values are changed to 0
Assignment operators
= += -= *= /= %= ^= **=
The regular operator
~!~ Match Regular expression/mismatched regular expression
logical operators
|| && logical OR Logical AND
Relational operators
< <= > >= = =
Other operators
$ field Reference
Space String link character
?: three mesh operator
Whether a key value exists in the ln array
awk Regular:
^ Beginning Locator
$ line Footer Locator
. Match any single character
* Match 0 or more leading characters (including carriage return)
+ Match 1 or more leading characters
? Match 0 or 1 leading characters
[] matches any one of the characters in the specified character group/^[ab]
[^] matches any character not in the specified character group
() sub-expression
| Or
\ escape Character
~,!~ a matching or mismatched conditional statement
X{M} x character repeats m times
X{m,} x characters repeat at least m times
X{m,n} x character repeats at least m times but not up to N times (requires parameter-posix or--re-interval)
This article is from the "Migrant Brother blog" blog, please be sure to keep this source http://mingongge.blog.51cto.com/2429897/1981142
Linux system commands the Three Musketeers of awk