Shell-awk editor

One, awk overview

1. Working principle

Read the text line by line, separated by space or tab by default, save the separated fields to built-in variables, and execute editing commands according to the mode or condition.

The sed command is often used to process a whole line, while awk tends to divide a line into multiple "fields" and then process it. The reading of awk information is also read line by line, and the execution result can be printed and displayed with the field data through the print function. In the process of using the awk command, you can use the logical operators "&&" to mean "and", "||" to mean "or", and "!" to mean "not"; you can also perform simple mathematical operations, such as +,- , *, /, %, ^ represent addition, subtraction, multiplication, division, remainder and power respectively.

2. Command format

awk option'mode or condition {operation}' file 1 file 2…
awk -f script file file 1 file 2…

3. Common built-in variables of awk (can be used directly) are as follows:

command meaning
FS Column separator. Specify the field separator for each line of text, the default is a space or a tab stop. Same as "-F"
NF The number of fields in the row currently being processed
NO The row number (ordinal number) of the row currently being processed
$0 The entire line content of the currently processed line
$n The nth field (column n) of the currently processed row
FILENAME File name being processed
RS Line separator. When awk reads data from a file, it will cut the data into many records according to the definition of RS, while awk only reads one record at a time for processing. The default value is'\n'

4. Related commands

①Output text by line

awk '{print}' 1.txt		                #输出所有内容
awk '{print $0}' 1.txt		            #输出所有内容
awk '(NR>=1)&&(NR<=5){print}'		    #输出1到5列内容
awk 'NR==1,NR==5{print}' 1.txt		    #输出1到5列内容
awk 'NR==1||NR==5{print}' 1.txt		    #输出第1和第5列内容
awk '(NR%2)==1{print}' 1.txt	   	    #输出奇数行内容
awk '(NR%2)==0{print}' 1.txt		    #输出偶数行内容
awk '/^root/{print}' /etc/passwd		#输出以root开头的行
awk '/bash$/{print}' /etc/passwd		#输出以bash结尾的行
awk 'BEGIN {x=0};/\/bin\/bash$/{x++};END{print x}' /etc/passwd  #统计/bin/bash结尾的的行数,等同于grep -c "/bin/bash$" 

BEGIN mode means that before processing the specified text, you need to perform the action specified in the BBGIN mode: awk then process the specified text, and then execute the action specified in the ENoD mode. END{} statement block, often put the print result Waiting sentence
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

②Output by field

awk -F ":" 'NR==1,NR==5{print $1,$3,$5}' /etc/passwd         #输出1到5行的1,3,5列
awk -F ":" '$3<5{print $1,$3,$5}' /etc/passwd                #输出1,3,5列,第三列小于5的行
awk -F ":" '!($3<200){print}' /etc/passwd                    #输出全部列,第三列不小于200的行
awk 'BEGIN {FS=":"};{if($3>=200){print}}' /etc/passwd        #先处理完BEGIN的内容,再打印文本里面的内容
awk -F ":" '{max=($3>$4)?$3:$4;{print max}}' /etc/passwd		
#($3>$4)?$3:$4三元运算符,如果第3个字段的值大于第4个字段的值,则把第3个字段的值赋给max,否则第4个字段的值赋给max
awk -F ":" '$7~"/bash"{print $1}' /etc/passwd	              #输出以冒号分隔且第7个字段中包含/bash的行的第1个字段
awk -F ":" '($1~"root")&&(NF==7){print $1,$2}' /etc/passwd    #输出第1个字段中包含root且有7个字段的行的第1、2个字段
awk -F ":" '($7!="/bin/bash")&&($7!="/sbin/nologin"){print}' /etc/passwd	 
#输出第7个字段既不为/bin/bash,也不为/sbin/nologin的所有行
awk -F ":" '{print NR,$0}' /etc/passwd		                  #输出每行内容和行号,每处理完一条记录,NR值加1

Insert picture description here
Insert picture description here
Insert picture description here

③Call Shell commands through pipes and double quotes

echo $PATH | awk 'BEGIN{RS=":"};END{print NR}'              #统计以冒号分隔的文本段落数,END{}语句块中,往往会放入打印结果等语句
awk -F: '/bash$/{print | "wc -l"}' /etc/passwd		        #调用wc -l命令统计bash的用户个数,等同于grep -c "bash$" 
free -m | awk '/Mem:/ {print int($3/($3+$4)*100)}'          #查看当前内存使用百分比
top -b -n 1 | grep Cpu | awk -F ',' '{print $4}' | awk '{print $1}'		#查看当前CPU空闲率(-b -n 1 表示只需要1次的输出结果)
date -d "$(awk -F "." '{print $1}' /proc/uptime) second ago" +"%F %H:%M:%S"		
#显示上次系统重启时间,等同于uptime;second ago为显示多少秒前的时间,+"%F %H:%M:%S"等同于+"%Y-%m-%d %H:%M:%S"的时间格式
awk 'BEGIN {while ("w" | getline) n++ ; {print n-2}"%"}'	 #调用w命令,并用来统计在线用户数
awk 'BEGIN {"hostname" | getline ; {print $0}}'			     #调用 hostname,并输出当前的主机名
seq 10 | awk '{print $0; getline}'   
seq 10 | awk '{getline; print $0}'

Insert picture description here
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/s15212790607/article/details/114845049