1、grep、egrep、fgrep 在文件中查找字符串(筛选)
语法
- grep 模式 文件名列表
常用选项:
-n 显示时每行前面显示行号
-v 显示所有不包含模式的行,反选
-i 忽略大小写
egrep 模式是扩展的正则表达式ERE
fgrep 快速搜索指定字符串,而不是按照模式搜索
正则表达式单字符
- 特定字符 例如:'a' 、'x'等
- 范围字符
- 数字字符 [0-9] [259] 中括号内的任一字符
- 小写字符 [a-z]
- 大写字符 [A-Z]
- 反向字符^ 例如:[^0-9]表示非数字字符,[^0]表示0之外的其他字符
- 任意字符. (注:[.]和\. 中的点表示'.'本身这个字符)
正则表达式的其他符号
- 边界字符
- 头字符^ 例如:^root表示以root开头的内容,注意:[^]表示'^'本身
- 尾字符$ 例如:root$表示以root结尾的内容
- 查找空行 grep '^$'
- 元字符
- \w 匹配任何字类字符(数字、字母、下划线),包括下划线[A-Za-z0-9_]
- \W 匹配任何非字类字符 [^A-Za-z0-9_]
- \b 代表单词的分隔
正则表达式字符组合
- 字符串 例如:'hello'、'he..o'、'[a-z]'
- 重复
- * 零次或多次匹配前面的字符 或子表达式
- + 一次或多次匹配前面的字符 或子表达式 (需要加\转义,负责'+'只表示加号本身)
- ? 零次或一次匹配前面的字符 或子表达式
- 重复特定次数{n,m} 至少重复n次,至多重复m次
- *:{0,}
- +:{1,}
- ?:{0,1}
- 任意字符串 .*
- 逻辑的表示 |
例子
查找当前目录下的所有文件夹
[root@localhost 桌面]# ll
总用量 36
-rw-r--r--. 1 root root 9 12月 3 14:55 123.txt
-rw-r--r--. 1 root root 26 12月 3 14:49 abc.txt
-rw-r--r--. 1 root root 39 12月 3 14:31 date.txt
-rw-r--r--. 1 root root 8349 11月 27 02:15 t2.txt
drwxr-xr-x. 2 root root 4096 12月 3 16:01 test
-rw-r--r--. 1 root root 388 11月 27 00:50 ttt.txt
-rw-r--r--. 1 root root 796 11月 27 02:16 tx.txt
[root@localhost 桌面]# ll | grep '^d'
drwxr-xr-x. 2 root root 4096 12月 3 16:01 test
2、sed 流编辑(加工)
原理
从文件或管道输入中,读取一行到模式空间,进行sed命令处理,处理完成后将结果输出到屏幕,接着读取下一行到模式空间,以此循环。sed不会改变源文件的内容。
用法
- 命令行格式
sed [options] 'command' file(s) #常用options: -e -n ;command正则表达,用于定位
sed -e '命令1' -e '命令2' -e '命令3' 文件名列表
- 脚本格式
sed -f 命令文件 文件名列表
定位行
- x; /pattern/ 定位第x行;定位与pattern匹配的行
- x,y; /pattern1/,/pattern2/ 定位第x至y行的内容;定位与pattern1匹配的行到与pattern2匹配的行之间的内容
- x,y! 定位第x至y行之外的内容
- first~step 定位第first开始,间隔为step的行
编辑命令
- a 追加,向匹配行后面插入内容;'3a hhh'在第三行追加字符hhh
- c 更改,更改匹配行的内容
- i 插入,向匹配行前插入内容
- d 删除,删除匹配的内容
- s 替换,替换掉匹配的内容
- p 打印,打印出匹配的内容,通常与-n选项合用
- = 打印被匹配的行
模式描述中增加\( 和 \),不影响匹配括号本身
高级命令
- {} 多个sed命令,用分号;分开
- n 加载下一行到模式
- & 替换固定的字符串
- 元字符 \u \l 首字母大小写转换 \U \L 整个单词大小写转换
- ()
- r 复制指定文件插入到匹配行,不会修改源文件
- w 复制匹配行拷贝到指定文件里,会修改指定文件的内容
- q 退出sed
使用例子
ttt.txt的内容如下
[root@localhost 桌面]# cat ttt.txt
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
打印ttt.txt的第1-3行的内容
[root@localhost 桌面]# sed -n '1,3p' ttt.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
打印2-5行之外的内容
[root@localhost 桌面]# sed -n '2,5!p' ttt.txt
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologi
打印第起始行包含‘sync’,结束行包含‘halt’,之间的内容
[root@localhost 桌面]# sed -n '/sync/,/halt/p' ttt.txt
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@localhost 桌面]# nl ttt.txt | sed -n '1~2p'
1 root:x:0:0:root:/root:/bin/bash
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
在第5行之后增加内容
[root@localhost 桌面]# sed '5a ======' ttt.txt
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
在第5行插入内容
[root@localhost 桌面]# nl ttt.txt | sed '5i ====='
1 root:x:0:0:root:/root:/bin/bash
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
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 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
将第10行的内容替换为hhhhhhhhhhh,第二条命令中的 \ 表示对空格进行转义
[root@localhost 桌面]# nl ttt.txt | sed '10c hhhhhhhhhhh'
1 root:x:0:0:root:/root:/bin/bash
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
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
hhhhhhhhhhh
[root@localhost 桌面]# nl ttt.txt | sed '10c \ hhhhhhhhhhh'
1 root:x:0:0:root:/root:/bin/bash
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
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
hhhhhhhhhhh
[root@localhost 桌面]#
删除包含shutdown的行
[root@localhost 桌面]# nl ttt.txt | sed '/shutdown/d'
1 root:x:0:0:root:/root:/bin/bash
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
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
将spool替换成spoooool
[root@localhost 桌面]# sed 's/spool/spoooool/' ttt.txt
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/spoooool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spoooool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spoooool/uucp:/sbin/nologin
将下写字母m替换成大写字母M,第二个命令的g表示全局替换,如果不加g则只会对每行第一次 匹配到的内容进行替换
[root@localhost 桌面]# sed 's/m/M/' ttt.txt
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
[root@localhost 桌面]#
[root@localhost 桌面]# sed 's/m/M/g' ttt.txt
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
Mail:x:8:12:Mail:/var/spool/Mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
通过替换获取本机ip地址
[root@localhost 桌面]# ifconfig eth0 | sed '/inet /p'
eth0 Link encap:Ethernet HWaddr 00:0C:29:B2:32:2B
inet addr:192.168.65.128 Bcast:192.168.65.255 Mask:255.255.255.0
inet addr:192.168.65.128 Bcast:192.168.65.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feb2:322b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:87 errors:0 dropped:0 overruns:0 frame:0
TX packets:20 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12744 (12.4 KiB) TX bytes:1408 (1.3 KiB)
[root@localhost 桌面]# ifconfig eth0 | sed -n '/inet /p'
inet addr:192.168.65.128 Bcast:192.168.65.255 Mask:255.255.255.0
[root@localhost 桌面]#
[root@localhost 桌面]# ifconfig eth0 | sed -n '/inet /p' | sed 's/inet.*r://'
192.168.65.128 Bcast:192.168.65.255 Mask:255.255.255.0
[root@localhost 桌面]#
[root@localhost 桌面]# ifconfig eth0 | sed -n '/inet /p' | sed 's/inet.*r://' | sed 's/B.*$//'
192.168.65.128
n命令的使用。第一个命令,读取一行,打印一行,加载下一行到模式;第二个命令,读取一行,加载下一行到模式,打印此时模式里的行。
[root@localhost 桌面]# nl ttt.txt | sed -n '{p;n}'
1 root:x:0:0:root:/root:/bin/bash
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[root@localhost 桌面]#
[root@localhost 桌面]# nl ttt.txt | sed -n '{n;p}'
2 bin:x:1:1:bin:/bin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
8 halt:x:7:0:halt:/sbin:/sbin/halt
10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
&命令的使用,&符号表示前面正则表达式匹配到的内容。
第一个命令表示在第一列后面加上空格,即将第一列的内容替换为 第一列的内容加上空格。
第二个命令中的\u表示首字母转换为大写
[root@localhost 桌面]# sed 's/^[a-z_]\+/& /' ttt.txt
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
shutdown :x:6:0:shutdown:/sbin:/sbin/shutdown
halt :x:7:0:halt:/sbin:/sbin/halt
mail :x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp :x:10:14:uucp:/var/spool/uucp:/sbin/nologin
[root@localhost 桌面]# sed 's/^[a-z_]\+/\u& /' ttt.txt
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
Shutdown :x:6:0:shutdown:/sbin:/sbin/shutdown
Halt :x:7:0:halt:/sbin:/sbin/halt
Mail :x:8:12:mail:/var/spool/mail:/sbin/nologin
Uucp :x:10:14:uucp:/var/spool/uucp:/sbin/nologin
替换字符串中的\1 \2 \3分别表示第一个、第二个、第三个括号中匹配到的内容
//将日期格式‘月-日-年’ 改为 ‘年.月.日’
[root@localhost 桌面]# cat date.txt
2-1-2001
2-2-2002
12-3-2023
11-30-2023
[root@localhost 桌面]# sed 's/\([0-9]\+\)-\([0-9]\+\)-\([0-9][0-9]*\)/\3.\1.\2/g' date.txt
2001.2.1
2002.2.2
2023.12.3
2023.11.30
r、w命令的使用
[root@localhost 桌面]# cat abc.txt
aaaaaaaa
bbbbbbbb
ccccccc
[root@localhost 桌面]# cat 123.txt
1111111
2222222
3333333
//把123.txt的内容插入到abc的第一行之后
[root@localhost 桌面]# sed '1r 123.txt' abc.txt
aaaaaaaa
1111111
2222222
3333333
bbbbbbbb
ccccccc
[root@localhost 桌面]# cat abc.txt
aaaaaaaa
bbbbbbbb
ccccccc
[root@localhost 桌面]# cat 123.txt
1111111
2222222
3333333
//把abc.txt的第二行复制到123.txt文件中
[root@localhost 桌面]# sed '2w 123.txt' abc.txt
aaaaaaaa
bbbbbbbb
ccccccc
[root@localhost 桌面]# cat abc.txt
aaaaaaaa
bbbbbbbb
ccccccc
[root@localhost 桌面]# cat 123.txt
bbbbbbbb
[root@localhost 桌面]#
q命令,找到符合条件的行之后就退出sed,不在读取下一行的内容
[root@localhost 桌面]# sed '/shutdown/q' ttt.txt
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@localhost 桌面]#
3、awk
awk处理方式
- 一次处理一行内容
- 对每一行进行切片处理,符合条件的行,执行相应的动作
- 输入文件的每行作为一个“记录”,变量NR就是行号,变量NF是每行的域的数量
- 每行用空格分隔开的部分,叫做记录的“域”
- 内置变量$1是第一域内容,依次,$2是第二域内容
- 特别地,$0指的是整个行的内容
用法
- 命令行格式
- awk [option] 'command' file(s) #每一行都执行command中的命令
- awk [option] '/pattern/{awk操作命令}' file(s) #与pattern匹配的行,执行{} 里的命令
- 脚本格式 awk -f awk_script_file file(s)
- command就是程序,多段程序间用空格或分号隔开
描述条件的方法
使用与C语言类似的关系算符 < <= == != > >=
使用与C语言类似的逻辑算符 || && !
正则表达式的模式匹配 /regexpr/
特殊的条件:
不指定任何条件,则对文本所有行执行动作
BEGIN 开始处理文本所有行之前执行动作
END 文本所有行执行之后执行动作
描述“动作”时,简单的用法
自定义变量
加减乘除等算数逻辑运算
正则表达式匹配运算符(用作条件判断) ~ !~
例:$2 ~ "[1-9][0-9]*" 要求第二列与正则表达式匹配
流程控制:条件判断if 循环控制for
打印:print 变量1,变量2,……
printf("格式串",变量1,变量2,……)
例子
ttt.txt文件的内容如下
[root@localhost 桌面]# cat ttt.txt
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
打印文件的 第三个列,-F ':' 表示以冒号作为分隔符,如果不指定-F选项,则默认以空格分隔
[root@localhost 桌面]# awk -F ':' '{print $3}' ttt.txt
0
1
2
3
4
5
6
7
8
10
打印文件的行号,每行的列数 、第一列的内容、第三列的内容
[root@localhost 桌面]# awk -F ':' '{print "lineNumber:" NR "\tcolNumber:" NF "\tcol_1:" $1 "\tcol_3:" $3}' ttt.txt
lineNumber:1 colNumber:7 col_1:root col_3:0
lineNumber:2 colNumber:7 col_1:bin col_3:1
lineNumber:3 colNumber:7 col_1:daemon col_3:2
lineNumber:4 colNumber:7 col_1:adm col_3:3
lineNumber:5 colNumber:7 col_1:lp col_3:4
lineNumber:6 colNumber:7 col_1:sync col_3:5
lineNumber:7 colNumber:7 col_1:shutdown col_3:6
lineNumber:8 colNumber:7 col_1:halt col_3:7
lineNumber:9 colNumber:7 col_1:mail col_3:8
lineNumber:10 colNumber:7 col_1:uucp col_3:10
如果第三列的数字大于等于7,则打印行号和整列的内容。判断条件可以在命令执行时判断,也可以在命令执行前先判断,如下两种 写法等效:
[root@localhost 桌面]# awk -F ':' '{if($3>=7) print "line:" NR "\t" $0}' ttt.txt
line:8 halt:x:7:0:halt:/sbin:/sbin/halt
line:9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
line:10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
[root@localhost 桌面]# awk -F ':' '$3>=7{print $0}' ttt.txt
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
打印出包含var的行的内容,/var/表示要找到包含var的行
[root@localhost 桌面]# awk -F ':' '/var/{print $0}' ttt.txt
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
使用正则表达式,如/^s/表示匹配以s开头的行,下面的语句$0~/^s/表示第一个字段以s开头,!~则表示相反的意思
[root@localhost 桌面]# awk -F ':' '/^s/{print $0}' ttt.txt
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@localhost 桌面]# awk -F ':' '$0~/var/{print $0}' ttt.txt
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
[root@localhost 桌面]# awk -F ':' '$0!~/var/{print $0}' ttt.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
例2:BEGIN、END的使用
文件内容如下:
[root@localhost 桌面]# ps -ef | tail >> tx.txt
[root@localhost 桌面]# cat tx.txt
root 3128 3119 0 01:10 ? 00:00:00 python /usr/share/ibus/ui/gtk/main.py
root 3130 1 0 01:10 ? 00:00:00 /usr/libexec/ibus-x11 --kill-daemon
root 3131 3119 0 01:10 ? 00:00:00 /usr/libexec/ibus-engine-pinyin --ibus
root 3145 1 0 01:10 ? 00:00:00 /usr/libexec/gvfsd-burn --spawner :1.8 /org/gtk/gvfs/exec_spaw/1
root 3147 1 0 01:10 ? 00:00:00 /usr/libexec/gvfsd-metadata
root 3152 1 0 01:10 ? 00:00:03 /usr/bin/gnome-terminal -x /bin/sh -c cd '/root/桌面' && exec $SHELL
root 3153 3152 0 01:10 ? 00:00:00 gnome-pty-helper
root 3154 3152 0 01:10 pts/0 00:00:00 /bin/bash
root 3395 3154 0 02:16 pts/0 00:00:00 ps -ef
root 3396 3154 0 02:16 pts/0 00:00:00 tail
打印进程的字段名,在表头和表尾。文件是以空格分隔的,所以不用加 -F 选项。
[root@localhost 桌面]# awk 'BEGIN{print "UID \t PID \t CMD"}{print $1" \t "$2" \t "$8}END{print "UID \t PID \t CMD"}' tx.txt
UID PID CMD
root 3128 python
root 3130 /usr/libexec/ibus-x11
root 3131 /usr/libexec/ibus-engine-pinyin
root 3145 /usr/libexec/gvfsd-burn
root 3147 /usr/libexec/gvfsd-metadata
root 3152 /usr/bin/gnome-terminal
root 3153 gnome-pty-helper
root 3154 /bin/bash
root 3395 ps
root 3396 tail
UID PID CMD
例3:求当前文件夹下的所有文件大小之和
[root@localhost 桌面]# ll
总用量 20
-rw-r--r--. 1 root root 8349 11月 27 02:15 t2.txt
-rw-r--r--. 1 root root 388 11月 27 00:50 ttt.txt
-rw-r--r--. 1 root root 796 11月 27 02:16 tx.txt
[root@localhost 桌面]#
[root@localhost 桌面]# ll | awk 'BEGIN{size=0}{size = size + $5}END{print "size:"size}'
size:9533
例4:netstat -anp 命令结果中,分别统计LISTEN和CONNETED的数量
[root@localhost 桌面]# netstat -anp | awk '$6~/CONNECTED|LISTEN/{sum[$6]++}END{for (i in sum) print i,sum[i]}'
LISTEN 10
CONNECTED 530
[root@localhost 桌面]# netstat -anp | awk '$6~/CONNECTED|LISTEN/{sum[$6]++}END{for (i in sum) printf("%s : %d\n", i,sum[i])}'
LISTEN : 10
CONNECTED : 530