文章目录
- 1. grep 过滤
- 2. sed 增删改查 替换
- 3. awk 擅长取列
- 4. 总结&排除空行
1. grep 过滤
grep #过滤 文本搜索工具 给过滤的字符加上颜色 支持正则 |
选项:
-n #显示过滤出来的内容所在文件的行号
-v #排除,取反
-c #统计过滤出来的内容的总行数
-i #过滤的时候忽略大小写
-o #只显示你要过滤的内容
-w #精确匹配 只过滤你要过滤的单词,而不是包含这个单词的字符串
-r #递归过滤 针对目录进行操作
-A #显示出你要过滤的内容及向下多少行的内容
-B #显示出你要过滤的内容及向上多少行的内容
-C #显示出你要过滤的内容向上向下各多少行
^ #以什么开头
$ #以什么为结尾
-E #支持扩展正则 ==== egrep
| # 或者 扩展正则
^$ #空行 排除存在空格或者tab键的空行
. #任意一个字符 排除换行符
* #前面的字符出现0次或者0次以上
.* #所有
[[email protected] ~]# grep "root" passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# grep -n 'root' passwd #显示过滤出来的内容所在文件的行号
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# grep -v 'root' passwd #排除包含'root'内容
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
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
[[email protected] ~]# grep -c 'root' passwd #统计过滤出来的内容的行数
2
[[email protected] ~]# cat file.txt
root
roottt
oldboy
olirl
oldgirl
ROOT
[[email protected] ~]# grep -i 'root' file.txt #过滤内容时忽略大小写
root
roottt
ROOT
[[email protected] ~]# grep -o 'root' passwd #只显示过滤的内容
root
root
root
root
[[email protected] ~]# grep 'root' file.txt
root
roottt
[[email protected] ~]# grep -w 'root' file.txt #只过滤你要过滤的单词而不是包含这个单词的字符串
root
[[email protected] ~]# grep -r 'root' ./ #递归过滤,针对目录的操作
./.bash_history:find root
./.bash_history:find /root/
./.bash_history:find /root -type f
./.bash_history:find /root -type f -name "^.txt"
./.bash_history:[[email protected] ~]# ll
./.bash_history:-rw-r--r--. 1 root root 38607 Jul 9 2013 7f6f8291jw1ee8c5j55rzj21hc0u0gwq.jpg
./.bash_history:-rw-r--r--. 1 root root 974 Jul 10 11:00 passwd
./.bash_history:drwxr-xr-x. 2 root root 6 Jul 10 12:04 test
./.bash_history:[[email protected] ~]#
./test.txt:root:x:0:0:root:/root:/bin/bash
./passwd:root:x:0:0:root:/root:/bin/bash
./passwd:operator:x:11:0:operator:/root:/sbin/nologin
./file.txt:root
./file.txt:roottt
[[email protected] ~]# cat file.txt
root
roottt
oldboy
olirl
oldgirl
ROOT
[[email protected] ~]# grep -A 2 'olirl' file.txt #显示过滤的内容及向下2行的内容
olirl
oldgirl
ROOT
[[email protected] ~]# grep -B 2 'olirl' file.txt #显示过滤的内容及向上2行的内容
roottt
oldboy
olirl
[[email protected] ~]# grep -C 2 'olirl' file.txt #显示过滤的内容及向上向下各多少行
roottt
oldboy
olirl
oldgirl
ROOT
#过滤root或者mail的字符串 -E 支持扩展正则的使用 | 或者的意思
[[email protected] ~]# grep -E 'root|mail' passwd
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
rootttt
2. sed 增删改查 替换
sed #擅长增删改查 替换 后向引用
选项:
-n #取消默认输出 和内部命令 p 组合使用 sed -n 'xxp'
-r #支持扩展正则使用 | 's###g'
-i #真正的改变文件内容
-e #允许多项编辑
内部指令:
p #print 打印
d # 删除 排除 取反
a #追加 将内容追加指定内容的后面
i #插入 将内容插入到指定的内容的前面
s #替换
g #全局
i #忽略大小写
\n #换行符
\t #tab键
= #显示行号
#查 过滤 p
[[email protected] ~]# sed -n '/root/p' passwd #打印过滤出来的内容 取消默认输出
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# sed -n '/^root/p' passwd #打印以root开头的行 ^ 以什么开头
root:x:0:0:root:/root:/bin/bash
[[email protected] ~]# sed -nr '/root|sshd/p' passwd #打印含root和sshd的内容
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
#打印不连续的内容
[[email protected] ~]# sed -n '/sync/p;/mail/p' passwd
sync:x:5:0:sync:/sbin:/bin/sync
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
#打印某个字符串到某个字符串的所有内容
[[email protected] ~]# sed -n '/sync/,/mail/p' 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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
#打印单行
[[email protected] ~]# sed -n '1p' passwd
root:x:0:0:root:/root:/bin/bash
#打印连续的行
[[email protected] ~]# sed -n '1,4p' 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
#打印不连续的行
[[email protected] ~]# sed -n '1p;5p' passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
#删 排除 d
[[email protected] ~]# cat -n test.txt
1 sync:x:5:0:sync:/sbin:/bin/sync
2 word
3 helld word
4 tail:root;mail;root
5 wordmagic
[[email protected] ~]# sed '/root/d' test.txt #排除包含root字符串的行
sync:x:5:0:sync:/sbin:/bin/sync
word
helld word
wordmagic
#删除不连续的字符串 删除多个字符串 以 ; 号隔开
[[email protected] ~]# sed '/root/d;/helld/d' test.txt
sync:x:5:0:sync:/sbin:/bin/sync
word
wordmagic
[[email protected] ~]# sed '/root/d;/sync/d;/helld/d' test.txt
word
wordmagic
[[email protected] ~]# sed '/root/d;1d' test.txt #删除包含root的行并删除第一行
word
helld word
wordmagic
#删除连续的字符串 以 , 隔开
[[email protected] ~]# sed '/sync/,/helld/d' test.txt
tail:root;mail;root
wordmagic
#删除单行
[[email protected] ~]# sed '1d' test.txt
word
helld word
tail:root;mail;root
wordmagic
#删除连续的行
[[email protected] ~]# sed '1,3d' test.txt
tail:root;mail;root
wordmagic
#删除不连续的行
[[email protected] ~]# sed '1d;3d' test.txt
word
tail:root;mail;root
wordmagic
#删除第一行到最后一行 $ 表示结尾
[[email protected] ~]# sed '1,$d' test.txt
#真正改变文件的内容 选项-i #添加或删除内容不会显示出来
[[email protected] ~]# sed -i '1d;3d' test.txt #删除第一和第三行内容
[[email protected] ~]# cat test.txt
word
tail:root;mail;root
wordmagic
[[email protected] ~]# sed '3ahello word' test.txt #追加内容到第三行后面
[[email protected] ~]# cat -n test.txt
1 word
2 tail:root;mail;root
3 wordmagic
4 hello word
[[email protected] ~]# sed -i '$ayasuo' test.txt #将内容追加至底部
[[email protected] ~]# tail -1 test.txt
yasuo
[[email protected] ~]# sed -ri '2s#(.*)#\1 zzc#g' test.txt #在行的尾巴添加内容
[[email protected] ~]# cat test.txt
word
tail:root;mail;root zzc
oldboy
wordmagic
hello word
oldgirl
yasuo
#增 a 添加内容到文件中
#将内容追加第三行后面
[[email protected] ~]# sed '3ahello word' test.txt | cat -n
1 word
2 tail:root;mail;root
3 oldboy
4 hello word
5 wordmagic
6 hello word
7 oldgirl
8 yasuo
#将内容追加到文件的底部 $ 表示最底部
[[email protected] ~]# sed '$aoldgirl' test.txt | cat -n
1 word
2 tail:root;mail;root
3 oldboy
4 wordmagic
5 hello word
6 oldgirl
7 yasuo
8 oldgirl
#追加多行内容到文件中 \n 换行符 \t tab键
1.#将内容追加第二行后面并换行
[[email protected] ~]# sed '2adog\ncat' test.txt | cat -n # \n 换行符
1 word
2 tail:root;mail;root
3 dog
4 cat
5 oldboy
6 wordmagic
7 hello word
8 oldgirl
9 yasuo
2.#将内容追加第三行后面并加上空格
[[email protected] ~]# sed '3ayasuo\truiwen' test.txt | cat -n # \t TAB键
1 word
2 tail:root;mail;root
3 oldboy
4 yasuo ruiwen
5 wordmagic
6 hello word
7 oldgirl
8 yasuo
3.#根据字符串进行追加内容 追加的内容在下一行
[[email protected] ~]# sed '/oldboy/azzc' test.txt | cat -n
1 word
2 tail:root;mail;root
3 oldboy
4 zzc
5 wordmagic
6 hello word
7 oldgirl
8 yasuo
#插入 i
[[email protected] ~]# cat -n test.txt
1 word
2 tail:root;mail;root
3 oldboy
4 wordmagic
5 hello word
6 oldgirl
7 yasuo
1.#在第一行的前面插入内容
[[email protected] ~]# sed '1iruiwen' test.txt | cat -n
1 ruiwen
2 word
3 tail:root;mail;root
4 oldboy
5 wordmagic
6 hello word
7 oldgirl
8 yasuo
[[email protected] ~]# sed '1iyasuo\truiwen' test.txt | cat -n # \t TAB键
1 yasuo ruiwen
2 word
3 tail:root;mail;root
4 oldboy
5 wordmagic
6 hello word
7 oldgirl
8 yasuo
2.#在第三行前面插入内容
[[email protected] ~]# sed '3imimu' test.txt | cat -n
1 word
2 tail:root;mail;root
3 mimu
4 oldboy
5 wordmagic
6 hello word
7 oldgirl
8 yasuo
3.#在最后一行的前面插入内容
[[email protected] ~]# sed '$ibook' test.txt | cat -n
1 word
2 tail:root;mail;root
3 oldboy
4 wordmagic
5 hello word
6 oldgirl
7 book
8 yasuo
4.#插入多行内容
[[email protected] ~]# sed '1iruiwen\nyasuo' test.txt | cat -n # \n 换行符
1 ruiwen
2 yasuo
3 word
4 tail:root;mail;root
5 oldboy
6 wordmagic
7 hello word
8 oldgirl
9 yasuo
[[email protected] ~]# sed '1iyasuo\truiwen' test.txt | cat -n # \t TAB键
1 yasuo ruiwen
2 word
3 tail:root;mail;root
4 oldboy
5 wordmagic
6 hello word
7 oldgirl
8 yasuo
5.根据字符串插入内容
[[email protected] ~]# sed '/hello/izzc' test.txt | cat -n
1 word
2 tail:root;mail;root
3 oldboy
4 wordmagic
5 zzc
6 hello word
7 oldgirl
8 yasuo
#根据字符串oldboy追加内容并根据字符串hello插入内容
[[email protected] ~]# sed '/oldboy/a亚索' test.txt | sed '/hello/i你好'
word
tail:root;mail;root
oldboy
亚索
wordmagic
你好
hello word
oldgirl
yasuo
#多向编辑 -e
[[email protected] ~]# sed -e '/oldboy/a亚索' -e '/hello/i你好' test.txt
word
tail:root;mail;root
oldboy
亚索
wordmagic
你好
hello word
oldgirl
yasuo
#改 替换 's#xx#xx#g' s 替换 g 全局 i 忽略大小写
[[email protected] ~]# cat test.txt
word
tail:root;mail;root
oldboy
wordmagic
hello word
oldgirl
yasuo
#全局替换
[[email protected] ~]# sed 's#word#zzc#g' test.txt #把所有word替换成zzc
zzc
tail:root;mail;root
oldboy
zzcmagic
hello zzc
oldgirl
yasuo
#针对某一行进行替换
#只把第一行word替换成zzc
[[email protected] ~]# sed '1s#word#zzc#g' test.txt
zzc
tail:root;mail;root
oldboy
wordmagic
hello word
oldgirl
yasuo
#替换多行内容
#把第一行到第四行的word替换成root
[[email protected] ~]# sed '1,4s#word#root#g' test.txt
root
tail:root;mail;root
oldboy
rootmagic
hello word
oldgirl
yasuo
#替换的时候,忽略大小写 's#xx#xx#gi'
[[email protected] ~]# echo WORD >> test.txt
[[email protected] ~]# cat test.txt
word
tail:root;mail;root
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
[[email protected] ~]# sed 's#word#zzc#gi' test.txt # i 替换时忽略大小写
zzc
tail:root;mail;root
oldboy
zzcmagic
hello zzc
oldgirl
yasuo
zzc
#替换第五行到最后一行的内容忽略大小写
[[email protected] ~]# cat -n test.txt
1 word
2 tail:root;mail;root
3 oldboy
4 wordmagic
5 hello word
6 oldgirl
7 yasuo
8 WORD
[[email protected] ~]# sed '5,$s#word#zzc#gi' test.txt | cat -n
1 word
2 tail:root;mail;root
3 oldboy
4 wordmagic
5 hello zzc
6 oldgirl
7 yasuo
8 zzc
#把包含root字符串行再进行替换操作
[[email protected] ~]# sed '/root/s#mail#zzc#g' test.txt
word
tail:root;zzc;root
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
#把以root开头的行再进行替换操作
[[email protected] ~]# sed '/^root/s#bin#good#g' test.txt
word
tail:root;mail;root
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
root:x:0:0:root:/root:/good/bash
#把文件中每个root后面加上abc
[[email protected] ~]# sed 's#root#rootabc#g' test.txt
word
tail:rootabc;mail;rootabc
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
rootabc:x:0:0:rootabc:/rootabc:/bin/bash
#给文件的每一行前面加上注释 #
[[email protected] ~]# sed 's#^#\##g' test.txt # \临时把#的含义取消单纯当做一个字符
#word
#tail:root;mail;root
#oldboy
#wordmagic
#hello word
#oldgirl
#yasuo
#WORD
#root:x:0:0:root:/root:/bin/bash
#把所有的#删除掉
[[email protected] ~]# sed 's#\###g' test.txt
word
tail:root;mail;root
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
root:x:0:0:root:/root:/bin/bash
#将某个字符全部删除
[[email protected] ~]# sed 's#root##g' test.txt
word
tail:;mail;
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
:x:0:0::/:/bin/bash
#排除空行 删除空行
^ #以什么开头
$ #以什么为结尾
^$ #空行 排除有空格或者tab键的空行
[[email protected] ~]# grep -v '^$' sshd_config #排除空行
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
[[email protected] ~]# sed '/^$/d' sshd_config #删除空行
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
#后向引用 () 扩展正则
前期定义 后期调用 \1 \2 第一个括号里面的内容 用 \1 第二个括号就是\2
[[email protected] ~]# sed -r '2s#(.*)#\1 zzc#g' test.txt
word
tail:root;mail;root zzc
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
root:x:0:0:root:/root:/bin/bash
#取IP地址
[[email protected] ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::5169:baec:f4cd:6fb9 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:02:d2:3a txqueuelen 1000 (Ethernet)
RX packets 19638 bytes 1668558 (1.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10764 bytes 1209273 (1.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[[email protected] ~]# ifconfig eth0 | sed -n '2p'
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
[[email protected] ~]# ifconfig eth0 | sed -n '2p' | sed -r 's#(^.*et)(.*)(n.*$)#\2#g'
10.0.0.100
[[email protected] ~]# ifconfig eth0 | sed -nr '2s#(^.*et)(.*)(n.*$)#\2#gp'
10.0.0.100
[[email protected] ~]# ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:02:d2:3a brd ff:ff:ff:ff:ff:ff
inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::5169:baec:f4cd:6fb9/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[[email protected] ~]# ip a s eth0 | sed -n '3p'
inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
[[email protected] ~]# ip a s eth0 | sed -n '3p' | sed -r 's#(^.*et)(.*)(/.*$)#\2#g'
10.0.0.100
[[email protected] ~]# ip a s eth0 | sed -nr '3s#(^.*et)(.*)(/.*$)#\2#gp'
10.0.0.100
#将passwd文件中的第一列与第七列的位置进行调换
[[email protected] ~]# cat 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
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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
[[email protected] ~]# sed -nr 's#(^.*)(:x.*:)(.*)#\3\2\1#gp' passwd
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp
/bin/sync:x:5:0:sync:/sbin:sync
/sbin/shutdown:x:6:0:shutdown:/sbin:shutdown
/sbin/halt:x:7:0:halt:/sbin:halt
/sbin/nologin:x:8:12:mail:/var/spool/mail:mail
/sbin/nologin:x:11:0:operator:/root:operator
/sbin/nologin:x:12:100:games:/usr/games:games
/sbin/nologin:x:14:50:FTP User:/var/ftp:ftp
/sbin/nologin:x:99:99:Nobody:/:nobody
#打印行号
[[email protected] ~]# sed = test.txt
1
word
2
tail:root;mail;root zzc
3
oldboy
4
wordmagic
5
hello word
6
oldgirl
7
yasuo
3. awk 擅长取列
awk #擅长取列 计算 数组 函数 一种编程语言
选项:
-F #指定分隔符 默认以空白字符为分隔符
-v #指定内部变量
内部命令 内部变量
Fs #指定输入分隔符
OFS #指定输出分隔符
NR #行号
NF #最后一列 为第几列
$0 #完整的一行内容
$n # n 是数字 表示取出第几列 多列用逗号分割
$NF #显示最后一列的内容
#查 过滤
[[email protected] ~]# awk '/root/' passwd #过滤出包含root的内容
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# awk '/root/;/adm/' passwd #过滤出包含root和adm的内容
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# awk '/root|adm/' passwd #过滤出包含root和adm的内容
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# awk '/adm/,/mail/' passwd #过滤出包含adm到mail的内容
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
#取行
[[email protected] ~]# awk 'NR==1' passwd #取第一行
root:x:0:0:root:/root:/bin/bash
[[email protected] ~]# awk 'NR==1,NR==3' 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
[[email protected] ~]# awk 'NR==1;NR==3' passwd #取第一行和第三行
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[[email protected] ~]# cat -n test.txt
1 word
2 tail:root;mail;root zzc
3 oldboy
4 wordmagic
5 hello word
6 oldgirl
7 yasuo
8 WORD
9 root:x:0:0:root:/root:/bin/bash
[[email protected] ~]# awk 'NR>4' test.txt #取第四行后面的内容不包含第四行
hello word
oldgirl
yasuo
WORD
root:x:0:0:root:/root:/bin/bash
[[email protected] ~]# awk 'NR<4' test.txt #取第四行前面的内容不包含第四行
word
tail:root;mail;root zzc
oldboy
[[email protected] ~]# awk 'NR<=4' test.txt #取包含第四行前面的内容
word
tail:root;mail;root zzc
oldboy
wordmagic
[[email protected] ~]# awk 'NR>=4' test.txt #取包含第四行很后面的内容
wordmagic
hello word
oldgirl
yasuo
WORD
root:x:0:0:root:/root:/bin/bash
#&& 并且
[[email protected] ~]# awk 'NR>2 && NR<5' test.txt #取第三行到第四行的内容
oldboy
wordmagic
#|| 或者
[[email protected] ~]# awk 'NR<2 || NR>6' test.txt #取小于第二行或取大于第六行的内容
word
yasuo
WORD
root:x:0:0:root:/root:/bin/bash
#打印整个文件内容 {print $0}
[[email protected] ~]# awk '{print $0}' test.txt
word
tail:root;mail;root zzc
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
root:x:0:0:root:/root:/bin/bash
#给文件内容加上行号
[[email protected] ~]# awk '{print NR,$0}' test.txt
1 word
2 tail:root;mail;root zzc
3 oldboy
4 wordmagic
5 hello word
6 oldgirl
7 yasuo
8 WORD
9 root:x:0:0:root:/root:/bin/bash
#取反 排除 !
[[email protected] ~]# awk '!/root/' test.txt #排除包含root的内容
word
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
[[email protected] ~]# awk 'NR!=1' test.txt #排除第一行内容
tail:root;mail;root zzc
oldboy
wordmagic
hello word
oldgirl
yasuo
WORD
root:x:0:0:root:/root:/bin/bash
# 取列 -F #分隔符的变量 $n # n 是数字 表示取出第几列 多列用逗号分割
#以 :为分隔符取第一列内容
[[email protected] ~]# awk -F '[:]' '{print $1}' passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
#以 :为分隔符取第一列和第二列内容
[[email protected] ~]# awk -F '[:]' '{print $1,$2}' passwd
root x
bin x
daemon x
adm x
lp x
sync x
shutdown x
halt x
mail x
operator x
games x
ftp x
nobody x
systemd-network x
dbus x
polkitd x
sshd x
postfix x
#以 :为分隔符取最后一列内容 $NF #最后一列
[[email protected] ~]# awk -F '[:]' '{print $NF}' passwd
/bin/bash
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/sync
/sbin/shutdown
/sbin/halt
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
# $(NF-1) 倒数第二列
[[email protected] ~]# awk -F '[:]' '{print $(NF-1)}' passwd
/root
/bin
/sbin
/var/adm
/var/spool/lpd
/sbin
/sbin
/sbin
/var/spool/mail
/root
/usr/games
/var/ftp
/
/
/
/
/var/empty/sshd
/var/spool/postfix
#默认分隔符为 空白字符
[[email protected] ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::3310:9d15:9ee4:43e8 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:eb:ea:8d txqueuelen 1000 (Ethernet)
RX packets 15508 bytes 1698801 (1.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10471 bytes 1145384 (1.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[[email protected] ~]# ifconfig eth0 | awk 'NR==2'
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
[[email protected] ~]# ifconfig eth0 | awk 'NR==2' | awk '{print $2}'
10.0.0.100
[[email protected] ~]# ifconfig eth0 | awk 'NR==2{print $2}'
10.0.0.100
#指定多个分隔符
[[email protected] ~]# ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:eb:ea:8d brd ff:ff:ff:ff:ff:ff
inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::3310:9d15:9ee4:43e8/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[[email protected] ~]# ip a s eth0 | awk 'NR==3'
inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
[[email protected] ~]# ip a s eth0 | awk 'NR==3' | awk -F '[ /]' '{print $6}'
10.0.0.100
[[email protected] ~]# ip a s eth0 | awk 'NR==3' | awk -F '[ /]*' '{print $3}'
10.0.0.100
[[email protected] ~]# echo " // inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0" >ip.txt
[[email protected] ~]# cat ip.txt
// inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
[[email protected] ~]# awk -F '[ /]' '{print $11}' ip.txt
10.0.0.100
[[email protected] ~]# awk -F '[ /]*' '{print $3}' ip.txt
10.0.0.100
#指定分隔符 使用双引号引起来 "[]"
[[email protected] ~]# awk -F: '{print $1":"$2}' passwd
root:x
bin:x
daemon:x
adm:x
lp:x
sync:x
shutdown:x
halt:x
mail:x
operator:x
games:x
ftp:x
nobody:x
systemd-network:x
dbus:x
polkitd:x
sshd:x
postfix:x
[[email protected] ~]# awk -F: '{print $1"脱产10期"$2}' passwd
root脱产10期x
bin脱产10期x
daemon脱产10期x
adm脱产10期x
lp脱产10期x
sync脱产10期x
shutdown脱产10期x
halt脱产10期x
mail脱产10期x
operator脱产10期x
games脱产10期x
ftp脱产10期x
nobody脱产10期x
systemd-network脱产10期x
dbus脱产10期x
polkitd脱产10期x
sshd脱产10期x
postfix脱产10期x
[[email protected] ~]# awk -F: -vOFS=":" '{a=$1;$1=$NF;$NF=a;print}' passwd
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
rootroot
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp
/bin/sync:x:5:0:sync:/sbin:sync
/sbin/shutdown:x:6:0:shutdown:/sbin:shutdown
/sbin/halt:x:7:0:halt:/sbin:halt
/sbin/nologin:x:8:12:mail:/var/spool/mail:mail
/sbin/nologin:x:11:0:operator:/root:operator
/sbin/nologin:x:12:100:games:/usr/games:games
/sbin/nologin:x:14:50:FTP User:/var/ftp:ftp
/sbin/nologin:x:99:99:Nobody:/:nobody
/sbin/nologin:x:192:192:systemd Network Management:/:systemd-network
/sbin/nologin:x:81:81:System message bus:/:dbus
/sbin/nologin:x:999:998:User for polkitd:/:polkitd
/sbin/nologin:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:tss
/sbin/nologin:x:173:173::/etc/abrt:abrt
/sbin/nologin:x:74:74:Privilege-separated SSH:/var/empty/sshd:sshd
/sbin/nologin:x:89:89::/var/spool/postfix:postfix
/sbin/nologin:x:38:38::/etc/ntp:ntp
/bin/bash:x:1000:1000::/home/zzc:zzc
4. 总结&排除空行
1. grep #过滤 给过滤出来的内容加上颜色
选项:
-n #给过滤出来的内容加上此行所在文件的行号
-i #忽略大小写
-c #统计的是行数 包含过滤字符串的行数
-v #排除 删除 取反
-o #只显示过滤出来的内容
-w #精确匹配 只匹配你要过滤的字符串,而不是过滤包含此字符串的字符串
-r #递归过滤 过滤多个文件 针对目录操作
-A #匹配过滤出来的内容向下多少行 后面加正整数
-B #匹配过滤出来的内容向上多少行
-C #匹配过滤出来的内容各向上向下多少行
-E #支持扩展正则使用 === egrep
^ #以什么开头
$ #以什么为结尾
^$ #空行 排除存在空格或者tab键的空行
. #匹配除换行符以外的任意一个字符
* #匹配前面的字符出现0次或者0次以上
.* #所有
| #或者 扩展正则
2. sed #擅长替换 增删改查 后向引用
选项:
-n #取消默认输出
-r #支持扩展正则
-i #真正的改变文件内容
-e #允许多项编辑 了解
内部命令:
p #打印
d #删除 排除 取反
s #替换
g #全局
i #忽略大小写
a #追加
i #插入
\n #换行符
\t #tab键
= #显示行号
后向引用:
[[email protected] ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::5169:baec:f4cd:6fb9 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:02:d2:3a txqueuelen 1000 (Ethernet)
RX packets 194 bytes 17674 (17.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 100 bytes 10541 (10.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[[email protected] ~]# ifconfig eth0 | sed -n '2p' | sed -r 's#(.*et )(.*)( n.*)#\2#g'
10.0.0.100
[[email protected] ~]# ifconfig eth0 | sed -nr '2s#(.*et )(.*)( n.*)#\2#gp'
10.0.0.100
[[email protected] ~]# ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:02:d2:3a brd ff:ff:ff:ff:ff:ff
inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::5169:baec:f4cd:6fb9/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[[email protected] ~]# ip a s eth0 | sed -n '3p' | sed -r 's#(.*et )(.*)(/.*)#\2#g'
10.0.0.100
[[email protected] ~]# ip a s eth0 | sed -nr '3s#(.*et )(.*)(/.*)#\2#gp'
10.0.0.100
[[email protected] ~]# stat /etc/hosts
File: ‘/etc/hosts’
Size: 158 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 17004494 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:net_conf_t:s0
Access: 2020-07-15 09:49:48.356726160 +0800
Modify: 2013-06-07 22:31:32.000000000 +0800
Change: 2020-07-06 09:29:45.882290124 +0800
Birth: -
[[email protected] ~]# stat /etc/hosts | sed -nr '4s#(.*\()(.*)(/-.*)#\2#gp'
0644
3. awk #擅长取列
选项:
-F #指定分隔符 默认的为空白字符为分隔符 FS
-v #指定内部变量
! #取反 排除
内部变量:
FS #指定输入分隔符
OFS #指定输出分隔符
NR #行号
NF #最后一列
$NF #显示最后一列
$0 #完整的一行内容
$n #n数字 指定取出的某一列
1. 排除空行 存在空格和tab键的空行
[[email protected] ~]# cat sshd_config
penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
1.# ^$ 空行 \s 空白字符 \t tab键
[[email protected] ~]# grep -v '^$' sshd_config
[[email protected] ~]# sed '/^$/d' sshd_config
[[email protected] ~]# awk '!/^$/' sshd_config
# \t tab键
[[email protected] ~]# grep -Pv '^[ \t]*$' sshd_config # -P 支持tab键
[[email protected] ~]# sed '/^[ \t]*$/d' sshd_config
[[email protected] ~]# awk '!/^[ \t]*$/' sshd_config
# \s 空白字符
[[email protected] ~]# grep -v '^\s*$' sshd_config
[[email protected] ~]# sed '/^\s*$/d' sshd_config
[[email protected] ~]# awk '!/^\s*$/' sshd_config
2.#排除空行和注释行 # 开头
[[email protected] ~]# grep -Ev '^$|^#' sshd_config
penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
[[email protected] ~]# grep -Ev '^\s*$|^#' sshd_config
penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
[[email protected] ~]# sed -r '/^[ \t]*$|^#/d' sshd_config
penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
[[email protected] ~]# sed -r '/^\s*$|^#/d' sshd_config
penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
[[email protected] ~]# awk '!/^\s*$|^#/' sshd_config
penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
[[email protected] ~]# awk '!/^[ \t]*$|^#/' sshd_config
penBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
3.#将/etc/passwd文件中的第一列和第七列位置进行调换 awk 等值替换
[[email protected] ~]# awk -F '[:]' '{a=$1;$1=$NF;$NF=a;print}' passwd | tr ' ' ':'
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp
/bin/sync:x:5:0:sync:/sbin:sync
/sbin/shutdown:x:6:0:shutdown:/sbin:shutdown
/sbin/halt:x:7:0:halt:/sbin:halt
/sbin/nologin:x:8:12:mail:/var/spool/mail:mail
/sbin/nologin:x:11:0:operator:/root:operator
/sbin/nologin:x:12:100:games:/usr/games:games
/sbin/nologin:x:14:50:FTP:User:/var/ftp:ftp
/sbin/nologin:x:99:99:Nobody:/:nobody
/sbin/nologin:x:192:192:systemd:Network:Management:/:systemd-network
/sbin/nologin:x:81:81:System:message:bus:/:dbus
/sbin/nologin:x:999:998:User:for:polkitd:/:polkitd
[[email protected] ~]# awk -F '[:]' -vOFS=":" '{a=$1;$1=$NF;$NF=a;print}' passwd
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp