Secretly learn shell script awk editor

awk editor

1. Working principle

  • 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.
  • Awk also reads the text line by line. By default, it is separated by a space or tab key as a separator. The separated fields are saved to built-in variables, and the editing commands are executed according to the mode or condition. The execution result can be displayed by printing 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 选项 '模式或条件 {操作}' 文件 1 文件 2 …
awk -f 脚本文件 文件 1 文件 2 …
awk常见的内建变量(可直接用)如下所示:
FS:列分割符。指定每行文本的字段分隔符,默认为空格或制表位。与"-F"作用相同
NF:当前处理的行的字段个数。
NR:当前处理的行的行号(序数)。
$0:当前处理的行的整行内容。
$n:当前处理行的第n个字段(第n列)。
FILENAME:被处理的文件名。
RS:行分隔符。awk从文件上读取资料时,将根据RS的定义把资料切割成许多条记录,而awk一次仅读入一条记录,以进行处理。预设值是'\n'

Three, use the awk editor (example)

1. Output text by line

awk '{print}' awk.txt				    #输出所有内容
awk '{print $0}' awk.txt 				#输出所有内容

Insert picture description here

awk 'NR==1,NR==3{print}' awk.txt		#输出第 1~3 行内容
awk '(NR>=1)&&(NR<=3){print}' awk.txt	#输出第 1~3 行内容

Insert picture description here

awk 'NR==1||NR==3{print}' awk.txt		#输出第1行、第3行内容

Insert picture description here

awk '(NR%2)==1{print}' awk.txt 		#输出所有奇数行的内容
awk '(NR%2)==0{print}' awk.txt		#输出所有偶数行的内容

Insert picture description here

awk '/^root/{print}' /etc/passwd		#输出以 root 开头的行

Insert picture description here

awk '/nologin$/{print}' /etc/passwd		#输出以 nologin 结尾的行

Insert picture description here

#统计以/bin/bash 结尾的行数,等同于 grep -c "/bin/bash$" /etc/passwd
awk 'BEGIN {x=0};/\/bin\/bash$/{x++};END {print x}' /etc/passwd		
BEGIN模式表示,在处理指定的文本之前,需要先执行BEGIN模式中指定的动作;awk再处理指定的文本,之后再执行END模式中指定的动作,END{
    
    }语句块中,往往会放入打印结果等语句

Insert picture description here

2. Output text by field

awk -F ":" '{print $3}' /etc/passwd 		#输出每行中以“:”分隔的第3个字段

Insert picture description here

awk -F ":" '{print $1,$3}' /etc/passwd		#输出每行中的第1、3个字段

Insert picture description here

awk -F ":" '$3<5{print $1,$3}' /etc/passwd	#输出第3个字段的值小于5的第1、3个字段内容

Insert picture description here

#输出第3个字段的值不小于200的行
awk -F ":" '!($3<200){print}' /etc/passwd
#输出第3个字段的值不小于200的行,先处理完BEGIN的内容,再打印文本里面的内容
awk 'BEGIN {FS=":"};{if($3>=200){print}}' /etc/passwd

Insert picture description here

#($3>$4)?$3:$4三元运算符,如果第3个字段的值大于第4个字段的值,则把第3个字段的值赋给max,否则第4个字段的值赋给max
awk -F ":" '{max=($3>$4)?$3:$4;{print max}}' /etc/passwd		

Insert picture description here

#输出每行内容和行号,每处理完一条记录,NR值加1
awk -F ":" '{print NR,$0}' /etc/passwd

Insert picture description here

#输出以冒号分隔且第7个字段中包含/bash的行的第1个字段
awk -F ":" '$7~"/bash"{print $1}' /etc/passwd

Insert picture description here

#输出第1个字段中包含root且有7个字段的行的第1、2个字段
awk -F ":" '($1~"root")&&(NF==7){print $1,$2}' /etc/passwd

Insert picture description here

#输出第7个字段既不为/bin/bash,也不为/sbin/nologin的所有行
awk -F ":" '($7!="/bin/bash")&&($7!="/sbin/nologin"){print}' /etc/passwd

Insert picture description here

3. Invoke Shell commands through pipes and double quotes

#统计以冒号分隔的文本段落数,END{}语句块中,往往会放入打印结果等语句
echo $PATH | awk 'BEGIN{RS=":"};END{print NR}'

Insert picture description here

#调用 wc -l 命令统计使用 bash 的用户个数
awk -F: '/bash$/{print | "wc -l"}' /etc/passwd
grep -c "bash$" /etc/passwd  #效果一样

Insert picture description here

#查看当前内存使用百分比
free -m | awk '/Mem:/ {print int($3/($3+$4)*100)}'

Insert picture description here

#查看当前CPU空闲率,(-b -n 1 表示只需要1次的输出结果)
top -b -n 1 | grep Cpu | awk -F ',' '{print $4}' | awk '{print $1}'

Insert picture description here

#显示上次系统重启时间,等同于uptime;second ago为显示多少秒前的时间,+"%F %H:%M:%S"等同于+"%Y-%m-%d %H:%M:%S"的时间格式
date -d "$(awk -F "." '{print $1}' /proc/uptime) second ago" +"%F %H:%M:%S"

Insert picture description here

#调用w命令,并用来统计在线用户数
awk 'BEGIN {while ("w" | getline) n++ ; {print n-2}"%"}'

Insert picture description here

#调用 hostname,并输出当前的主机名
awk 'BEGIN {"hostname" | getline ; {print $0}}'

Insert picture description here
supplement:

  • When there is no redirection character "<" or "|" on the left and right of getline, getline acts on the current file and reads the first line of the current file to the variable var or $0 followed by it; because awk has already read a line before processing the getline , So the return result of getline is interlaced.
  • When there are redirection characters "<" or "|" on the left and right of getline, getline acts on the directional input file. Since the file is just opened and has not been read into a line by awk, only getline reads it, then getline returns this The first line of the file, not every other line.
    Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51326240/article/details/111817156