grep 强大的文本搜索工具
语法形式
grep [选项] '[word]' [参数]
选项
-v #反转查找;
-r #当指定要查找的是<目录>而非文件时,必须使用这项参数;
-E #识别扩展正则进行过滤;等同于egrep;
-w #过滤的内容两边必须是空格(类似于边界符)
------------------------------------------------------------
--color #对筛选出的WORD加颜色显示;建议设置永久别名;
-i #搜索时不区分大小写;
-n #搜索出的结果显示行号;
-o #展示匹配过程;
-c #统计单词出现的次数;
-A #过滤到内容往下2行;
-B #过滤到内容往上2行;
-C #过滤到内容上下各2行;
示例
#在/etc/passwd文件中过滤出root的行
[root@Dezyan ~]# grep 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
#不区分大小写过滤出 /etc/ssh/sshd_config 文件中包含`port 22`的行并输出行号
[root@Dezyan ~]# grep -i -n 'port 22' /etc/ssh/sshd_config
17:#Port 22
#排除(取反)/etc/selinux/config文件中的#和注释
[root@Dezyan ~]# grep -v '^#|^$' /etc/ssh/sshd_config
#统计word.txt文件中shutdown单词的个数
[root@Dezyan ~]# grep -c 'shutdown' word.txt
2
#显示文件word.txt中config的上下各两行
[root@Dezyan ~]# grep -C 2 'config' word.txt
test 测试
server 服务
configure 配置 config conf cfg
continue 继续
next 下一个
sed 功能强大的流式文本编辑器
- 语法形式
sed [选项] '[模式][动作]' [参数]
- 选项
-n #取消内存空间默认输出;不添加时sed命令会默认输出文件所有内容及匹配到的内容;
-r #使模式中的正则表达式支持扩展正则;
-i #对源文件进行修改;不添加时不会修改;
- 模式
number #按行查找,查找第n行
number或正则符号,number或正则符号 #查找n到m行
/字符串或正则表达式/ #模糊查询,查询包含此字符串的行
/字符串/,/字符串/ #匹配区间:查询两个字符串之间的内容(也可用正则)
n[动作];m[动作] #指定第n行和第m行进行操作
- 动作
p #输出打印过滤出的内容
d #删除过滤出的内容
a 字符串 #add 在……后追加xxx
i 字符串 #insert 在……前插入xxx
c 字符串 #replace 将……替换为xxx
--------------------------------------------------
s###g #1.其中,g代表全局替换
#2.s///g、s@@@g等与s###g效果相同
- 总结
命令 | 选项 | '模式 | 动作’ |
---|---|---|---|
sed | -n | n | p |
sed | -r | n,m | d |
sed | -i | // | a 内容 |
sed | //,// | i 内容 | |
sed | n[];m[] | c 内容 | |
sed | s###g | ||
sed | w file |
注意:
-
动作中有p,模式中必有n
-
在使用匹配区间( /开头字符串/,/结尾字符串/ )时,尽量使用文件内容中唯一的、不重复的字符串;
- 若只有一个开头,有两个甚至多个结尾时:输出的内容只会是开头到第一个结尾字符串之间的内容;
- 若只有开头,结尾字符串没有:输出的内容为开头字符串到文件末尾;
-
sed的后向引用
sed 's#(正则)(数字)(字符串)#\1\3#g' #\n获取第n个()中的内容
实例
#获取IP地址
[root@Dezyan ~]# ip add show ens33 | sed -rn '3s#.*et (.*)/24 .*$#\1#gp'
10.0.0.101
#批量创建用户test01..test03
[root@Dezyan ~]# echo test{01..03} | xargs -n1 | sed -r 's#(.*)#useradd \1#g' | bash
##批量创建用户test01..test03并设置密码为dingzhiyan1016
echo test{01..03} | xargs -n1 | sed -r 's#(.*)#useradd \1 ; echo dingzhiyan1016 | passwd --stdin \1#g' | bash
或
[root@Dezyan ~]# echo test11{01..03} | xargs -n1 | sed -r 's#(.*)#useradd \1 ; echo \1:dingzhiyan1016 | chpasswd #g' | bash
#批量删除用户
[root@Dezyan ~]# echo user{1..20} | xargs -n1 | sed -r 's#(.*)#userdel -r \1#g' | bash
- 示例
1.a.txt文件
[root@Dezyan ~]# cat -n a.txt
1 The first snow came.
2 How beautiful it was, falling so silently all day long,
3 all night long.
4 on the mountains, on the meadows,
5 on the roofs of the living, on the graves of the dead! A
2.查看a.txt文件第三行的内容
[root@Dezyan ~]# sed -n '3p' a.txt
all night long.
3.查看a.txt文件中第三行到结尾的内容
[root@Dezyan ~]# sed -n '3,$p' a.txt
all night long.
on the mountains, on the meadows,
on the roofs of the living, on the graves of the dead! A
4.查看a.txt中以o开头或以T开头的行
[root@Dezyan ~]# sed -rn '/^o|^T/p' a.txt
The first snow came.
on the mountains, on the meadows,
on the roofs of the living, on the graves of the dead! A
5.查看a.txt文件中包含so与meadows行之间的内容
[root@Dezyan ~]# sed -n '/so/,/meadows/p' a.txt
How beautiful it was, falling so silently all day long,
all night long.
on the mountains, on the meadows,
6.在a.txt文件1到3行后都添加一行Dezyan
[root@Dezyan ~]# sed '1,3i Dezyan' a.txt
Dezyan
The first snow came.
Dezyan
How beautiful it was, falling so silently all day long,
Dezyan
all night long.
on the mountains, on the meadows,
on the roofs of the living, on the graves of the dead! A
7.将a.txt文件中所有的(第一个)on替换为under
[root@Dezyan ~]# sed 's#on#under#g' a.txt (只替换第一个只需将g去掉即可)
The first snow came.
How beautiful it was, falling so silently all day lunderg,
all night lunderg.
under the mountains, under the meadows,
under the roofs of the living, under the graves of the dead! A
8.查找roofs所在行,并将其替换为floor,并且只显示替换行
[root@Dezyan ~]# sed -n '/roofs/s#roofs#floor#gp' a.txt
on the floor of the living, on the graves of the dead! A
9.删除文件amount.txt中的所有字母
[root@Dezyan testdir]# sed -r 's#[a-Z]##g' amount.txt