1.1 find查找命令
1、find命令说明
1. Linux find命令用来在指定目录下查找文件。
2. 任何位于参数之前的字符串都将被视为欲查找的目录名。
3. 如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。
-name # 按文件名查找 -size # 按文件大小查找 -perm # 按权限查找 -mtime n # 查找n天内修改内容的文件 -mmin n # 查找n分钟内修改内容的文件
2、find常用查找方法
# 查找当前目录下大于9M的文件详细信息 [root@linux-node1 /]# find . -size +9M | xargs ls -lh -rw-r--r-- 1 root root 24M Jul 7 04:18 ./aaa/Python-3.7.0/libpython3.7m.a -rwxr-xr-x 1 root root 14M Jul 7 04:19 ./aaa/Python-3.7.0/Programs/_testembed -rwxr-xr-x 1 root root 14M Jul 7 04:18 ./aaa/Python-3.7.0/python -rw-r--r-- 1 root root 22M Jul 6 23:53 ./aaa/Python-3.7.0.tgz -rw-------. 1 root root 47M Jan 7 2019 ./boot/initramfs-0-rescue-8b956f09fe0549c4b6182589acceab30.img -rw-------. 1 root root 21M Jan 7 2019 ./boot/initramfs-3.10.0-514.el7.x86_64.img -rw-------. 1 root root 14M Jan 7 2019 ./boot/initramfs-3.10.0-514.el7.x86_64kdump.img
[root@linux-node1 /]# find . -type f -name "*.log" -size +1M -exec cp -av {} /tmp ; cp: ‘./tmp/audit.log’ and ‘/tmp/audit.log’ are the same file cp: ‘./tmp/journal.log’ and ‘/tmp/journal.log’ are the same file
[root@linux-node1 /]# find /var -mtime +3 -mtime -5 /var/tmp /var/lib/yum/yumdb/l /var/lib/yum/yumdb/l/f20daac8f6b3893e42be72af22a5118848f
[root@linux-node1 /]# find . -mmin +1 -mmin -3 ./aa.py
1.2 grep指令
1、grep指令说明
1. grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来
2. grep搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。
^linux # 以linux开头的行 $php # 以php结尾的行 . # 匹配任意单字符 .+ # 匹配任意多个字符 .* # 匹配0个或多个字符(可有可无) [0-9a-z] # 匹配中括号内任意一个字符 [abc] # 表示匹配一个字符,这个字符必须是abc中的一个。 (linux)+ # 出现多次Linux单词 (web){2} # web出现两次以上 # 屏蔽转义
2、grep常用查找方法
[root@linux-node1 /]# grep -n 'root' /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin
[root@linux-node1 /]# grep -Ev "root|nologin" /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt
[root@linux-node1 /]# grep "root" /etc/{passwd,shadow} /etc/passwd:root:x:0:0:root:/root:/bin/bash /etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
[root@linux-node1 /]# grep -c root /etc/passwd 2
3、grep其他用法
[root@linux-node1 ~]# grep -E -v "^$|^#" /etc/nginx/nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; }
[root@linux-node1 ~]# seq 1 20 |grep -m 5 -E '[0-9]{2}' 10 11 12 13 14
[root@linux-node1 ~]# seq 1 20 |grep -c -E '[0-9]{2}' 11
[root@linux-node1 ~]# echo "a bc de" |xargs -n1 |grep '^b' bc
[root@linux-node1 ~]# echo "a ab abc abcd abcde" |xargs -n1 |grep -n 'de$' 5:abcde
[root@linux-node1 ~]# grep -r 'sshd' /etc --include *.conf /etc/sestatus.conf:/usr/sbin/sshd /etc/sestatus.conf:/usr/sbin/sshd
[root@linux-node1 ~]# seq 41 45 |grep -E '4[12]' 41 42
1.3 sed流编辑器,过滤和替换文本
1、sed指令说明
1. sed 命令将当前处理的行读入模式空间进行处理,处理完把结果输出,并清空模式空间。
2. 然后再将下一行读入模式空间进行处理输出,以此类推,直到最后一行。
3. 还有一个暂存空间,可以暂时存放一些处理的数据,但不能直接输出,只能放到模式空间输出。
4. 这两个空间其实就是在内存中初始化的一个内存区域,存放正在处理的数据和临时存放的数据
-n # 只列出sed处理的哪一行内容
-e # 直接在sed模式上进行sed动作编辑
-f # 直接将sed动作写在一个文件内
-r # 让sed指令支持扩展的正则表达式
'''1. 常用选项 ''' -n # 不打印模式空间 -e # 执行脚本、表达式来处理 -f # 执行动作从文件读取执行 -i # 修改原文件 -r # 使用扩展正则表达式 '''2. 常用命令 ''' s/regexp/replacement/ # 替换字符串 p # 打印当前模式空间 P # 打印模式空间的第一行 d # 删除模式空间,开始下一个循环 D # 删除模式空间的第一行,开始下一个循环 = # 打印当前行号 a ext # 当前行追加文本 i ext # 当前行上面插入文本 c ext # 所选行替换新文本 q # 立即退出 sed 脚本 r # 追加文本来自文件 w filename # 写入当前模式空间到文件 ! # 取反、 否定 '''3. 常用地址 ''' $ # 匹配最后一行 /regexp/ # 正则表达式匹配行 number # 只匹配指定行 addr1,addr2 # 开始匹配 addr1 行开始,直接 addr2 行结束 addr1,+N # 从 addr1 行开始,向后的 N 行 addr1,~N # 从 addr1 行开始,到 N 行结束
2、sed常用方法
[root@linux-node1 aaa]# nl /etc/passwd | sed '2,5d' 1 root:x:0:0:root:/root:/bin/bash 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8 halt:x:7:0:halt:/sbin:/sbin/halt 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10 operator:x:11:0:operator:/root:/sbin/nologin
[root@linux-node1 aaa]# nl /etc/passwd | sed '2,5c "new content"' 1 root:x:0:0:root:/root:/bin/bash "new content" 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8 halt:x:7:0:halt:/sbin:/sbin/halt 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10 operator:x:11:0:operator:/root:/sbin/nologin
[root@linux-node1 aaa]# nl /etc/passwd | sed '/root/d' 2 bin:x:1:1:bin:/bin:/sbin/nologin 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync
[root@linux-node1 aaa]# nl /etc/passwd | sed -e '3,$d' -e 's/root/mewusername/' 1 mewusername:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin -e '3,$d' # 删除/etc/passwd第三行到末尾的数据 -e 's/root/nuwusername/' # 将搜索到的内容 root替换成 newusername
3、sed匹配打印
[root@linux-node1 ~]# tail /etc/services |sed -n '/^blp5/p' blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator
[root@linux-node1 ~]# tail /etc/services |sed -n '1p' 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
[root@linux-node1 ~]# tail /etc/services |sed -n '1,3p' 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services
[root@linux-node1 ~]# seq 10 |sed -n '1~2p' 1 3 5 7 9
[root@linux-node1 ~]# tail /etc/services |sed -n '$p' matahari 49000/tcp # Matahari Broker
[root@linux-node1 ~]# tail /etc/services |sed -n '$!p' 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services
[root@linux-node1 ~]# tail /etc/services |sed -n '/^blp5/,/^com/p' blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw
[root@linux-node1 ~]# tail /etc/services |sed -n '/blp5/,$p' blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw
[root@linux-node1 ~]# a=1 [root@linux-node1 ~]# tail /etc/services |sed -n "$a,3p" 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services
4、sed匹配删除
[root@linux-node1 ~]# tail /etc/services |sed '/blp5/d' 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw
[root@linux-node1 ~]# tail /etc/services |sed '1d' isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw
[root@linux-node1 ~]# tail /etc/services |sed '1~2d' isnetserv 48128/tcp # Image Systems Network Services blp5 48129/tcp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw iqobject 48619/tcp # iqobject matahari 49000/tcp # Matahari Broker
[root@linux-node1 ~]# sed '/^#/d;/^$/d' /etc/nginx/nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; }
5、sed匹配替换
[root@linux-node1 ~]# tail /etc/services |sed 's/blp5/test/' 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services test 48129/tcp # Bloomberg locator test 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw
[root@linux-node1 ~]# tail /etc/services |sed -n 's/^blp5/test/p' test 48129/tcp # Bloomberg locator test 48129/udp # Bloomberg locator
[root@linux-node1 ~]# tail /etc/services |sed 's/48049/&.0/' 3gpp-cbsp 48049.0/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw
[root@linux-node1 ~]# tail /etc/services | sed '1,4s/blp5/test/' 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services test 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw iqobject 48619/tcp # iqobject iqobject 48619/udp # iqobject
[root@linux-node1 ~]# tail /etc/services | sed '/48129/tcp/s/blp5/test/' 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services test 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw iqobject 48619/tcp # iqobject iqobject 48619/udp # iqobject matahari 49000/tcp # Matahari Broker
[root@linux-node1 ~]# tail /etc/services |sed -e '1,2d' -e 's/blp5/test/' isnetserv 48128/udp # Image Systems Network Services test 48129/tcp # Bloomberg locator test 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw [root@linux-node1 ~]# tail /etc/services |sed '1,2d;s/blp5/test/' # 也可以使用分好分隔 isnetserv 48128/udp # Image Systems Network Services test 48129/tcp # Bloomberg locator test 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw iqobject 48619/tcp # iqobject
6、sed添加新内容
i: 匹配行上面添加
a: 匹配航下面添加
c: 将匹配航替换成新内容
[root@linux-node1 ~]# tail /etc/services |sed '/blp5/i est' #在 blp5 上一行添加 test 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services test blp5 48129/tcp # Bloomberg locator test blp5 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw
[root@linux-node1 ~]# tail /etc/services |sed '/blp5/a est' # 在 blp5 下一行添加 test 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services blp5 48129/tcp # Bloomberg locator test blp5 48129/udp # Bloomberg locator test com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw
[root@linux-node1 ~]# tail /etc/services |sed '/blp5/c est' 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services test test com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw
[root@linux-node1 ~]# tail /etc/services |sed '2a est' 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services test isnetserv 48128/udp # Image Systems Network Services blp5 48129/tcp # Bloomberg locator
7、文本操作
1 2 3 4 5 6 7 8 9
[root@linux-node1 ~]# tail /etc/services |sed '/blp5/r a.txt' 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services isnetserv 48128/udp # Image Systems Network Services blp5 48129/tcp # Bloomberg locator 1 2 3 4 5 6 7 8 9 blp5 48129/udp # Bloomberg locator 1 2 3 4 5 6 7 8 9 com-bardac-dw 48556/tcp # com-bardac-dw com-bardac-dw 48556/udp # com-bardac-dw iqobject 48619/tcp # iqobject iqobject 48619/udp # iqobject matahari 49000/tcp # Matahari Broker
[root@linux-node1 ~]# tail /etc/services |sed '/blp5/w b.txt' #只有下面两行被写入 blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator
1.4 awk指令
1、awk指令说明
1. awk是一种编程语言,用于在linux下对文本和数据进行处理
2. awk的处理文件和数据处理方式是逐行扫描,寻找到匹配的行,并在这些行上进行你想要的操作
3. 如果没有指定处理动作,则把匹配的行显示到屏幕上
// # 匹配代码块,可以是字符串或正则表达式 {} # 命令代码块,包含一条或多条命令 $0 # 表示整个当前行 $1 # 每行第一个字段 NF # 字段数量变量 NR # 每行的记录号,多文件记录递增 /[0-9][0-9]+/ # 两个或两个以上数字 /[0-9][0-9]*/ # 一个或一个以上数字 -F'[:#/]' # 定义三个分隔符 FNR # 与NR类似,不过多文件记录不递增,每个文件都从1开始 # 制表符 # 换行符 FS # BEGIN时定义分隔符 RS # 输入的记录分隔符, 默认为换行符(即文本是按一行一行输入) ~ # 匹配,与==相比不是精确比较 !~ # 不匹配,不精确比较 == # 等于,必须全部相等,精确比较 != # 不等于,精确比较 && # 逻辑与 || # 逻辑或 + # 匹配时表示1个或1个以上
2、awk常用指令
[root@linux-node1 aaa]# cat /etc/passwd |awk -F ':' '{print $1}' root bin daemon adm -F ':' # 使用 ":" 来分隔 '{print $1}' # 打印第一列的数据
[root@linux-node1 aaa]# cat /etc/passwd | awk -F ':' '{print $1" "$7}' root /bin/bash bin /sbin/nologin daemon /sbin/nologin adm /sbin/nologin lp /sbin/nologin sync /bin/sync '{print $1" "$7}' # 打印第一列和第七列数据,并以制表符分隔
[root@linux-node1 aaa]# awk -F ':' '/root/{print $1}' /etc/passwd root operator
[root@linux-node1 aaa]# w|awk 'NR==1{print $6}' 2
[root@linux-node1 aaa]# awk -F: 'NR==5 || NR==6{print}' /etc/passwd lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync
[root@linux-node1 aaa]# awk '!/mysql/' /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync
[root@linux-node1 aaa]# awk '/mysql|mail/{print}' /etc/passwd mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
3、awk基本语法
[root@linux-node1 ~]# tail -n3 /etc/services |awk -F'[ /]+' '{print $2}' # 以空格或斜线分隔 48619 48619 49000
[root@linux-node1 ~]# awk -v a=123 'BEGIN{print a}' 123
[root@linux-node1 ~]# tail -n3 /etc/services |awk 'BEGIN{print "服务 端口 描述"}{print $0}END{print "===结束==="}' 服务 端口 描述 iqobject 48619/tcp # iqobject iqobject 48619/udp # iqobject matahari 49000/tcp # Matahari Broker ===结束===
4、正则匹配
[root@linux-node1 ~]# tail /etc/services |awk '/^blp5/{print $0}' blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator
[root@linux-node1 ~]# tail /etc/services |awk '/^[a-z0-9]{8} /{print $0}' iqobject 48619/tcp # iqobject iqobject 48619/udp # iqobject matahari 49000/tcp # Matahari Broker
[root@linux-node1 ~]# tail /etc/services |awk '/blp5/ || /tcp/{print $0}' 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/tcp # Image Systems Network Services blp5 48129/tcp # Bloomberg locator
[root@linux-node1 ~]# awk '! /^#/ && ! /^$/{print $0}' /etc/nginx/nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn;
[root@linux-node1 ~]# tail /etc/services |awk '/^blp5/,/^com/' blp5 48129/tcp # Bloomberg locator blp5 48129/udp # Bloomberg locator com-bardac-dw 48556/tcp # com-bardac-dw
5、NF
[root@linux-node1 ~]# echo "a b c d e f" |awk '{print NF}' 6
[root@linux-node1 ~]# echo "a b c d e f" |awk '{print $1}' a
[root@linux-node1 ~]# echo "a b c d e f" |awk '{print $NF}' : 打印最后一行 f
[root@linux-node1 ~]# echo "a b c d e f" |awk '{print $(NF-1)}' :打印倒数第二行 e
[root@linux-node1 ~]# echo "a b c d e f" |awk '{$NF="";$(NF-1)="";print $0}' :排除最后两个字段 a b c d
6、NR
[root@linux-node1 ~]# tail -n5 /etc/services |awk '{print NR,$0}' :打印行号+内容 1 com-bardac-dw 48556/tcp # com-bardac-dw 2 com-bardac-dw 48556/udp # com-bardac-dw 3 iqobject 48619/tcp # iqobject
[root@linux-node1 ~]# tail -n5 /etc/services |awk 'NR==3{print $2}' :打印第三行第二列的值 48619/tcp
[root@linux-node1 ~]# tail -n5 /etc/services |awk 'NR<=3{print NR,$0}' :打印前三行 1 com-bardac-dw 48556/tcp # com-bardac-dw 2 com-bardac-dw 48556/udp # com-bardac-dw 3 iqobject 48619/tcp # iqobject
7、操作符
'''在 awk 中,有 3 种情况表达式为假:数字 0,空字符串和未定义的值''' [root@linux-node1 ~]# awk 'BEGIN{n=0;if(n)print "true";else print "false"}' [root@linux-node1 ~]# awk 'BEGIN{s="";if(s)print "true";else print "false"}' [root@linux-node1 ~]# awk 'BEGIN{if(s)print "true";else print "false"}'
[root@linux-node1 ~]# seq 3 |awk '{print $0*2}' : 乘法 2 4 6
[root@linux-node1 ~]# seq 3 |awk '{print $0/2}' :除法 0.5 1 1.5
[root@linux-node1 ~]# seq 5 |awk '$0%2==0{print $0}' :取余 2 4
[root@linux-node1 ~]# seq 5 |shuf |awk '{print $0|"sort"}' :先打乱再排序 1 2 3 4 5
8、流程控制 (if 语句 )
if (condition) statement [ else statement ]
[root@linux-node1 ~]# seq 5 |awk '{if($1==3)print $0}' :如果第一列的值等于3,打印出来 3
[root@linux-node1 ~]# echo "123abc#456cde 789aaa#aaabbb aaaa#eee666eeee " |xargs -n1 |awk -F# '{if($2~/[0-9]/)print $2}' # $2~/[0-9]/ (如果第二行匹配到数字就打印) 456cde eee666eeee
[root@linux-node1 ~]# seq 5 |awk '{if($0==3)print $0;else print "no"}' :双分支if no no 3 no no
[root@linux-node1 ~]# awk 'BEGIN{a["a"]=123}END{if("a" in a)print "yes"}' < /dev/null :判断数组成员 yes
[root@linux-node1 ~]# awk 'BEGIN{print 1==1?"yes":"no"}' :三目运算符 yes
9、多分支if、for循环、while语句
1 2 3 4 5 6 7 8 9
[root@linux-node1 ~]# awk '{i=1;while(i<=NF){print $i;i++}}' file 1 2 3 4 5 6 7 8 9
[root@linux-node1 ~]# awk '{for(i=1;i<=NF;i++)print $i}' file 1 2 3 4 5 6 7 8 9
[root@linux-node1 ~]# awk '{for(i=NF;i>=1;i--)print $i}' file 3 2 1 6 5 4 9 8 7
[root@linux-node1 ~]# awk 'BEGIN{for(i=1;i<=5;i++){if(i==3){break};print i}}' 1 2
[root@linux-node1 ~]# awk 'BEGIN{for(i=1;i<=5;i++){if(i==3){continue};print i}}' 1 2 4 5
10、数组
[root@linux-node1 ~]# awk 'BEGIN{a[0]="test";print a[0]}' test
[root@linux-node1 ~]# tail -n5 /etc/passwd |awk -F: '{a[NR]=$1}END{for(v in a)print a[v],v}' saltapi 4 nginx 5 sshd 1 chrony 2 tcpdump 3
[root@linux-node1 ~]# tail /etc/services |awk '{a[$1]++}END{for(v in a)print a[v],v}' 2 com-bardac-dw 1 3gpp-cbsp 2 iqobject 1 matahari 2 isnetserv 2 blp5
[root@linux-node1 ~]# netstat -antp |awk '/^tcp/{a[$6]++}END{for(v in a)print a[v],v}' 11 LISTEN 3 ESTABLISHED
[root@linux-node1 ~]# tail /etc/services |awk '{a[$1]++}END{for(v in a) if(a[v]>=2){print a[v],v}}' 2 com-bardac-dw 2 iqobject 2 isnetserv 2 blp5