shell常用命令(文本处理命令三剑客grep、sed、awk)

摘要:zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ grep -i 'false$' passwd_bk tss:x:106:111:TPM software stack,,,:/var/lib/tpm:

一、grep:文本的搜索、过滤二、sed:文本的增删改查三、awk:文本的数据处理并生成结构报告

选项和含义

-i:忽略大小写-c:只输出匹配行的数量-n:显示行号-r:递归搜索-E:支持拓展正则表达式-w:匹配整个单词-l:只列出匹配的文件名-F:不支持正则,按照字符串字面意思进行匹配grep默认只支持基础正则表达式(* ^ . $ [^])

1、查找文件下包含author关键字的所在行

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ grep -i 'author' demo_cpuinfo.sh #author: kobe

2、查找某个目录下包含author关键字的内容

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ grep -i -r 'author' ../../03shell/!:#author: kobe../03shell/demo_cpuinfo.sh:#author: kobe../02shell/demo_case.sh:#author: kobe../02shell/check_ip.sh:# author:kobe../02shell/demo_for1.sh:#author: kobe../02shell/demo_for2.sh:#author: kobe../02shell/define_function.sh:#author: zhilong../02shell/demo_if.sh:#author: kobe../02shell/!:#author: kobe../02shell/demo_for.sh:#author: kobe../02shell/demo_while.sh:#author: kobe../02shell/read.sh:# author:kobe../02shell/check_ip1.sh:# author:kobe../02shell/define_script.sh:#author: zhilong

3、查找某个目录下包含author关键字的路径

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ grep -i -r -l 'author' ../../03shell/!../03shell/demo_cpuinfo.sh../02shell/demo_case.sh../02shell/check_ip.sh../02shell/demo_for1.sh../02shell/demo_for2.sh../02shell/define_function.sh../02shell/demo_if.sh../02shell/!../02shell/demo_for.sh../02shell/demo_while.sh../02shell/read.sh../02shell/check_ip1.sh../02shell/define_script.sh

4、查找以root开头的所有行

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ grep -i '^root' passwd_bk root:x:0:0:root:/root:/bin/bash

5、查找以false结尾的所有行

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ grep -i 'false$' passwd_bk tss:x:106:111:TPM software stack,,,:/var/lib/tpm:/bin/falsespeech-dispatcher:x:114:29:Speech Dispatcher,,,:/run/speech-dispatcher:/bin/falsehplip:x:119:7:HPLIP system user,,,:/run/hplip:/bin/falsewhoopsie:x:120:125::/nonexistent:/bin/falsegnome-initial-setup:x:124:65534::/run/gnome-initial-setup/:/bin/falsegdm:x:125:130:Gnome Display Manager:/var/lib/gdm3:/bin/falsemysql:x:127:134:MySQL Server,,,:/nonexistent:/bin/false

6、查找包含“oo”的内容

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ grep -i 'oo' passwd_bk root:x:0:0:root:/root:/bin/bashlp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologinnews:x:9:9:news:/var/spool/news:/usr/sbin/nologinuucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologinkernoops:x:116:65534:Kernel Oops Tracking Daemon,,,:/:/usr/sbin/nologinnm-openvpn:x:118:124:NetworkManager OpenVPN,,,:/var/lib/openvpn/chroot:/usr/sbin/nologinwhoopsie:x:120:125::/nonexistent:/bin/false

7、统计包含“oo”关键字的行数

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ grep -i -c 'oo' passwd_bk

8、过滤目标行以及下面的2行

-A2:表示过滤目标行及其下面的2行

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ grep --help | grep -A2 '\-E'-E, --extended-regexp 是扩展正则表达式-F, --fixed-strings 是字符串-G, --basic-regexp 是基本正则表达式

9、过滤目标行以及上面的2行

-B2:表示过滤目标行及其上面的2行

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ grep --help | grep -B2 '\-E'模式选择与解释:-E, --extended-regexp 是扩展正则表达式

10、过滤目标行以及上面下面的各2行

-C2:表示过滤目标行及其上面和下面的2行

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ grep --help | grep -C2 '\-E'模式选择与解释:-E, --extended-regexp 是扩展正则表达式-F, --fixed-strings 是字符串-G, --basic-regexp 是基本正则表达式

选项和含义

-n:只打印模式匹配的行
-f:加载存放动作的文件
-r:支持拓展正则
-i:直接修改文件

1、将第一行打印出来

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ sed -n '1 p' passwd_bk root:x:0:0:root:/root:/bin/bash

2、将第5行到第8行打印出来

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ sed -n '5,8 p' passwd_bk sync:x:4:65534:sync:/bin:/bin/syncgames:x:5:60:games:/usr/games:/usr/sbin/nologinman:x:6:12:man:/var/cache/man:/usr/sbin/nologin666666666这三新增的内容

3、将包含root的内容打印出来

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ sed -n '/root/ p' passwd_bk root:x:0:0:root:/root:/bin/bashnm-openvpn:x:118:124:NetworkManager OpenVPN,,,:/var/lib/openvpn/chroot:/usr/sbin/nologin

4、将以m开头的内容打印出来

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ sed -n '/^m/ p' passwd_bk man:x:6:12:man:/var/cache/man:/usr/sbin/nologinmail:x:8:8:mail:/var/mail:/usr/sbin/nologinmessagebus:x:103:106::/nonexistent:/usr/sbin/nologinmysql:x:127:134:MySQL Server,,,:/nonexistent:/bin/false

5、将以man开头到list开头中间的内容打印出来

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ sed -n '/^man/,/^list/ p' passwd_bk man:x:6:12:man:/var/cache/man:/usr/sbin/nologin666666666这三新增的内容lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologinmail:x:8:8:mail:/var/mail:/usr/sbin/nologinnews:x:9:9:news:/var/spool/news:/usr/sbin/nologinuucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologinproxy:x:13:13:proxy:/bin:/usr/sbin/nologinwww-data:x:33:33:www-data:/var/www:/usr/sbin/nologinbackup:x:34:34:backup:/var/backups:/usr/sbin/nologinlist:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin

6、在man开头的下面新增内容

a:新增内容

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ sed '/^man/ a 这是新增的内容' passwd_bk

7、在第3到5行前面新增内容

i:在前面新增内容

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ sed '3,5 i 这是新增的内容' sed.txt sssd:x:126:131:SSSD system user,,,:/var/lib/sss:/usr/sbin/nologinzhilong:x:1000:1000:zhilong,,,:/home/zhilong:/bin/bash这是新增的内容systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin这是新增的内容mysql:x:127:134:MySQL Server,,,:/nonexistent:/bin/false这是新增的内容redis:x:128:135::/var/lib/redis:/usr/sbin/nologin

8、将文件中内容读取之后插入到文本中包含bash关键字的后面

r:追加的文件来自哪里

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ sed '/bash/ r test.txt' sed.txt sssd:x:126:131:SSSD system user,,,:/var/lib/sss:/usr/sbin/nologinzhilong:x:1000:1000:zhilong,,,:/home/zhilong:/bin/bashsystemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologinmysql:x:127:134:MySQL Server,,,:/nonexistent:/bin/falseredis:x:128:135::/var/lib/redis:/usr/sbin/nologin6666666666688888888888

9、将文本中以uuidd开头的文本写入到test.txt文本中

w:将匹配内容写到指定文件

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ sed '/^uuidd/ w test.txt' sed.txt sssd:x:126:131:SSSD system user,,,:/var/lib/sss:/usr/sbin/nologinzhilong:x:1000:1000:zhilong,,,:/home/zhilong:/bin/bashsystemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologinmysql:x:127:134:MySQL Server,,,:/nonexistent:/bin/falseredis:x:128:135::/var/lib/redis:/usr/sbin/nologinzhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ cat test.txt redis:x:128:135::/var/lib/redis:/usr/sbin/nologin

10、将文本中包含“四”关键字的行删除

d:将指定内容删除

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ sed '/四/ d' test.txt redis:x:128:135::/var/lib/redis:/usr/sbin/nologin这是第二行这是第三行这是第五行这是第六行

11、将文本中包含五的行和空行删除

-r:支持拓展正则

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ sed -r '/五|^$/ d' test.txt redis:x:128:135::/var/lib/redis:/usr/sbin/nologin这是第二行这是第三行这是第四行这是第六行

12、将第2和3行删除

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ sed '2,3 d' sed.txt sssd:x:126:131:SSSD system user,,,:/var/lib/sss:/usr/sbin/nologinmysql:x:127:134:MySQL Server,,,:/nonexistent:/bin/falseredis:x:128:135::/var/lib/redis:/usr/sbin/nologin

13、删除第2到最后一行

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ sed '2,$ d' sed.txt sssd:x:126:131:SSSD system user,,,:/var/lib/sss:/usr/sbin/nologin

14、将小写zhilong改成大写ZHILONG(如果一行有多个zhilong,默认修改的是第一个zhilong)

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ sed s/zhilong/ZHILONG/ sed.txt sssd:x:126:131:SSSD system user,,,:/var/lib/sss:/usr/sbin/nologinZHILONG:x:1000:1000:zhilong,,,:/home/zhilong:/bin/bashsystemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologinmysql:x:127:134:MySQL Server,,,:/nonexistent:/bin/falseredis:x:128:135::/var/lib/redis:/usr/sbin/nologin

15、将第3到第9行,小写zhilong改成大写BASH(所有的ZHILONG)

g:全局修改
i:忽略大小写

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ sed '1,5 s/zhilong/ZHILONG/gi' sed.txt sssd:x:126:131:SSSD system user,,,:/var/lib/sss:/usr/sbin/nologinZHILONG:x:1000:1000:ZHILONG,,,:/home/ZHILONG:/bin/bashsystemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologinmysql:x:127:134:MySQL Server,,,:/nonexistent:/bin/falseredis:x:128:135::/var/lib/redis:/usr/sbin/nologin

16、将路径/usr/sbin/nologin改为、home/zhilong

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ sed '/^sssd/,/^redis/ s/\/usr\/sbin\/nologin/\/home\/zhilong/gi' sed.txt sssd:x:126:131:SSSD system user,,,:/var/lib/sss:/home/zhilongzhilong:x:1000:1000:zhilong,,,:/home/zhilong:/bin/bashsystemd-coredump:x:999:999:systemd Core Dumper:/:/home/zhilongmysql:x:127:134:MySQL Server,,,:/nonexistent:/bin/falseredis:x:128:135::/var/lib/redis:/home/zhilong

17、整行替换,将某一行的内容替换为其他的内容

c:整行替换

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ sed '/^sssd/ c "替换后的内容"' sed.txt "替换后的内容"zhilong:x:1000:1000:zhilong,,,:/home/zhilong:/bin/bashsystemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologinmysql:x:127:134:MySQL Server,,,:/nonexistent:/bin/falseredis:x:128:135::/var/lib/redis:/usr/sbin/nologin

awk ‘BEGIN{}’ pattern {commands} END{}’ file

BEGIN{}:处理数据之前执行,只执行一次pattern:匹配模式{commands}:处理的命令END{}:处理数据之后执行,只执行一次

内置变量

$0:整行内容$1-$n:当前行的第1-n个字段NF:当前行的字段数NR:当前行行号FS:输入字段分隔符,默认为空格或者tab键RS:输入行分隔符,默认为回车符-F:以什么为分隔符

1、打印文本中所有行的内容(空格)

$0:表示所有的内容

hilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ awk '{print $0}' awk.txt no name score gender age001 kobe 100 男 24 we002 jimmy 99 男 30 er003 curry 98 女 34 we

2、打印文本中第一列的内容(空格)

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ awk '{print $1}' awk.txt no001002003

3、打印出文本中最后一列的内容(空格)

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ awk '{print $NF}' awk.txt ageweerwe

4、打印出文本中倒数第二列的内容(空格)

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ awk '{print $(NF-1)}' awk.txt gender243034awk: run time error: negative field index $-1 FILENAME="awk.txt" FNR=5 NR=5

或者:

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ awk 'BEGIN{FS=":"} {print $1}' passwd_bk rootdaemonbinsyssyncgamesman666666666这三新增的内容

5、将包含www关键字的所在行的第3列取出

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ awk -F: '/www/ {print $3}' passwd_bk 33

6、将以list开头到syslog开头之间所有行的最后一列取出

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ awk -F: '/^list/,/^syslog/ {print $NF}' passwd_bk /usr/sbin/nologin/usr/sbin/nologin/usr/sbin/nologin/usr/sbin/nologin/usr/sbin/nologin/usr/sbin/nologin/usr/sbin/nologin/usr/sbin/nologin/usr/sbin/nologin

7、打印第一行中的第一列

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ awk -F: 'NR==1 {print $1}' passwd_bk root

8、打印第1到第5行中的第一列

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ awk -F: 'NR==1,NR==5 {print $1}' passwd_bk rootdaemonbinsyssync

9、将第一列和最后一列打印出来(左对齐)
printf格式符

%s:字符串%d:十进制数字%f:浮点数

修饰符

+:右对齐-:左对齐zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ awk -F: '{printf "%-15s%-20s\n",$1,$NF}' passwd_bk root /bin/bash daemon /usr/sbin/nologin bin /usr/sbin/nologin sys /usr/sbin/nologin sync /bin/sync games /usr/sbin/nologin man /usr/sbin/nologin

10、将第一列和最后一列打印出来(左对齐),并且加上标题

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ awk -F: 'BEGIN{printf "%-15s s\n","username","password"} {printf "%-15s s\n",$1,$NF}' passwd_bk username passwordroot /bin/bashdaemon /usr/sbin/nologinbin /usr/sbin/nologinsys /usr/sbin/nologinsync /bin/syncgames /usr/sbin/nologinman /usr/sbin/nologin

11、将第三列的数字大于100,并且只打印第一列和最后一列

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/03shell$ awk -F: 'BEGIN{printf "%-20s%-25s\n","USERNAME","ROOT"} $3>100 {printf "%-20s%-25s\n",$1,$NF; i++} END{printf "%-20s%-25d\n","TOTAL",i}' ../passwd_bk USERNAME ROOT root /bin/bash daemon /usr/sbin/nologin bin /usr/sbin/nologin sys /usr/sbin/nologin sync /bin/sync games /usr/sbin/nologin man /usr/sbin/nologin 666666666这三新增的内容666666666这三新增的内容lp /usr/sbin/nologin mail /usr/sbin/nologin news /usr/sbin/nologin uucp /usr/sbin/nologin proxy /usr/sbin/nologin www-data /usr/sbin/nologin backup /usr/sbin/nologin list /usr/sbin/nologin irc /usr/sbin/nologin gnats /usr/sbin/nologin systemd-network /usr/sbin/nologin TOTAL 19

课程咨询、考试辅导、学习资料——>助理V:glab-mary

来源:郭主任

相关推荐