Linux Three Musketeers boss awk

" God knows why I remember these commands .-- programming three minutes ."

Outline

awkWith seda command similar, but sedgood at taking the line, awkcommand good at taking column. (Based on knowledge of awka language, but we are only concerned about his handling of the text function, use good words can replace almost excel)
principle: generally each line traversing a file, and then separately for each line of the file is processed
usage:

awk [可选的命令行选项] 'BEGIN{命令 } pattern{ 命令 } END{ 命令 }'  文件名
复制代码

Print a few columns

$ echo 'I love you' | awk '{print $3 $2 $1}'
youloveI
复制代码

We'll string I love youpiped to the command awk, awk is equivalent to processing a file, the contents of the file is I love you, by default (there are between columns regardless of how many spaces will be treated as a space) through a space as a separator I love youto split into three up.
If the symbol is divided ., it can be used

$ echo '192.168.1.1' | awk -F "." '{print $2}'
168
复制代码

Conditions filter

We know that awk usage is so, then the pattern section how to use it?

awk [可选的命令行选项] 'BEGIN{命令 } pattern{ 命令 } END{ 命令 }'  文件名

$ cat score.txt
tom 60 60 60
kitty 90 95 87
jack 72 84 99
$ awk '$2>=90{print $0}' score.txt
kitty 90 95 87
复制代码

$ 2> = 90 indicates if the current value of the second row of the column is greater than 90, the processing of the current row, or not treated. To put it plainly pattern part is used to filter out rows from a file to be processed is processed, this part is empty on behalf of all the processing. Analyzing the results of pattern portions may be any conditional expression, for example, >,<,==,>=,<=,!=but also can use +,-,*,/complex arithmetic expressions conditional expression combining logic &&,||,!can also be used in. Further portions may also use pattern / regular / selected row to be processed.

Judgment statement

Judge sentence is written in pattern{ 命令 }command, he has the same effect filter condition, and he can also make a richer output

$ awk '{if($2>=90 )print $0}' score.txt
kitty 90 95 87
$ awk '{if($2>=90 )print $1,"优秀"; else print $1,"良好"}' score.txt
tom 良好
kitty 优秀
jack 良好
复制代码

BEGIN defined header

awk [可选的命令行选项] 'BEGIN{命令 } pattern{ 命令 } END{ 命令 }'  文件名
复制代码

Use as follows:

$ awk 'BEGIN{print "姓名 语文 数学 英语"}{printf "%-8s%-5d%-5d%-5d\n",$1,$2,$3,$4}' score.txt
姓名 语文数学英语
tom 60 60 60
kitty 90 95 87
jack 72 84 99
复制代码

Here we must note, I look good for the output format, made a left-aligned operation (% -8s left, width 8), printfusage, and c++similar.
Not only can be used to define the header, you can also do some variable initialization work, for example,

$ awk 'BEGIN{OFMT="%.2f";print 1.2567,12E-2}'
1.26 0.12
复制代码

Here OFMT is a built-in variable initialization digital output format to two decimal places.

Add TAIL END

And similar usage BEGIN

$ echo ok | awk '{print $1}END{print "end"}'
ok
end
复制代码

Data calculation

The place I want to enlarge the trick! The above knowledge you remember it?

$ awk 'BEGIN{print "姓名 语文 数学 英语 总成绩"; \
sum1=0;sum2=0;sum3=0;sumall=0} \
{printf "%5s%5d%5d%5d%5d\n",$1,$2,$3,$4,$2+$3+$4;\
sum1+=$2;sum2+=$3;sum3+=$4;sumall+=$2+$3+$4}\
END{printf "%5s%5d%5d%5d%5d\n","总成绩",sum1,sum2,sum3,sumall}'\
 score.txt
姓名 语文 数学 英语 总成绩
  tom 60 60 60 180
kitty 90 95 87 272
 jack 72 84 99 255
总成绩 222 239 246 707
复制代码

Because the command is too long, I end with a \symbolic line change. .

  • I BEGIN body in output header, and four variable is initialized to 0
  • pattern in my body every line of output, and accumulate operations
  • END output my body in the overall statistical results
    , of course, a normal person when using linux command input is not so much formatting symbols aligned, so the new command again
    column -t(God knows why I remember so much mess The command.)


Useful built-in variables

NF: Indicates the current line number of fields, and therefore $NFrepresents the last field
NR: indicates the current processing is the first few lines
FILENAME: current file name
OFMT: digital output format, the default is% .6g. After printing only represents six decimal places

$ awk -F ':' '{print NR ") " $1}' demo.txt
1) root
2) daemon
3) bin
4) sys
5) sync
复制代码

Built-in functions

awk defines a number of built-in functions, using awk to write a shell script is actually a good choice, but most of us are no longer applicable, the following is a commonly used functions

$ echo 1 2 | awk '{print $1+sqrt($2)}'

2.41421
复制代码

Random number, then the first set of random seed

rand () 0 <= n <1, srand ([expr]) | value the seed the rand function of the value of the parameter Expr, Expr parameter is omitted, or if the time of day is used.

$ echo 1 | awk 'BEGIN{srand()}{print rand()}'

0.929885
复制代码

String

Common system

Not commonly used arithmetic:


Recommended Reading

(Click on the title to jump to read)

Three Musketeers youngest grep linux

Linux sed Three Musketeers second child

Fun day three minutes Git (end)

I secretly dug a tunnel network, the company came close to being activated

If it helps do not forget to share with friends Oh ~


Guess you like

Origin blog.csdn.net/weixin_33709590/article/details/91388298