day13 linux三剑客之sed
1、grep忽略大小写 -i
2、grep查询/etc目录下有多少个文件包含root [root@localhost ~]# grep -R -l "root" /etc/ | wc -l
145
3、将/etc目录下所有的普通文件打包压缩至/tmp目录
[root@localhost tmp]# tar -czPf /tmp/etc.tar.gz $(find /etc/ -type f | xargs )
4、将etc下包含root的文件打包压缩到/tmp目录下
[root@localhost tmp]# tar -czPf /tmp/grep.tar.gz $(grep -R -l "root" /etc/ | xargs )
5、取反的正则表达式
[^]
6、或者的正则表达式
| []
7、匹配手机号的正则表达式
[root@localhost tmp]# echo "15517127878" | grep -E "(137|187|155)[0-9]{8}"
8、匹配IP的正则表达式
[root@localhost tmp]# ip a | grep -Eo "([0-9]{1,3}\.){3}[0-9]{1,3}"
9、将/etc/fstab文件中所有的注释行和空行过滤掉
[root@localhost tmp]# egrep -v "^$|^#" /etc/fstab
[root@localhost tmp]# egrep "^[^#]" /etc/fstab
[root@localhost ~]# grep -vE "^ *#|^$" /etc/fstab
linux三剑客之sed
1.sed
sed (流式编辑器):sed是操作、过滤和转换文本内容的强大工具。常用功能有对文件实现快速增删改查,其中查询的功能中最常用的2大功能是过滤(过滤指定字符串)和取行(取出指定行)。
1.sed命令
sed [参数] "[定位][指令]" 处理的文本路径
注:不指定定位,则默认处理全文
指令:
p:print,打印文本,通常与-n一起使用
d:delete,删除文本
参数:
-e:允许多项编辑
-n:取消默认输出,常与sed内置命令的p连用
-i:就地编辑文本,直接修改文件内容,而不是输出到终端,如果不适用-i选项,sed只是修改在内存中的数据,并不会影响磁盘上的文件
-r:支持扩展正则表达式(sed中的正则表达式必须放在两个//中间)
-f:指定定位规则的文件
案例
1、在文本中,打印第一行和第五行
[root@localhost ~]# sed -e "1p" -e "5p" 1.txt
2、在文本中,要求只打印第1,5,6三行
[root@localhost ~]# sed -n -e "1p" -e "5p" -e "6p" 1.txt
3、要求删除文件的第3行
[root@localhost ~]# sed -i "3d" 1.txt
4、删除/etc/fstab文件中所有的注释的行
[root@localhost ~]# sed -i -r "/^ *#|^$/d" /etc/fstab
5、删除1.txt的3行,打印第4行
[root@localhost ~]# sed -f 4.txt 1.txt
[root@localhost ~]# cat 4.txt
3d
4p
练习1:要求将/etc/passwd文件复制到/root/1.txt中,然后删除当中包含/sbin/nologin的行
cat /etc/passwd > /root/1.txt
sed -i -r "/\/sbin\/nologin/d" /root/1.txt
练习2:将/etc/nginx/nginx.conf文件中所有的注释的行(以#开头的行) 全部删除
sed -i -r "/^ *#/d" /etc/ngind/nginx.conf
2.sed+正则表达式(定位)
1.数字
1.固定定位
[root@localhost ~]# sed -n "2p" 1.txt
sync:x:5:0:sync:/sbin:/bin/sync
2.范围定位
[root@localhost ~]# sed -n "1,3p" 1.txt
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
halt:x:7:0:halt:/sbin:/sbin/halt
2.正则
正则表达式必须放在//之间
1.打印/etc/passwd文件中包含root的行
[root@localhost ~]# sed -n "/root/p" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
2.将包含空格的行打印出来
[root@localhost ~]# sed -n -r "/ +/p" /etc/passwd
3.数字加正则
正则匹配是非贪婪性的匹配
贪婪性匹配是匹配到了之后,不停止继续匹配,直到文件所有的内容全部匹配完毕
非贪婪性匹配是一旦匹配到了就停止匹配
1、在/etc/passwd文件中的第一行,到包含test的行,全部删除
[root@localhost ~]# sed -r "1,/test/d" /etc/passwd
2、删除从包含root的行到第5行的内容
[root@localhost ~]# sed -r "/root/,5d" /etc/passwd
3、从包含root的行删除到包含ftp的行
[root@localhost ~]# sed -r "/root/,/ftp/d" /etc/passwd
4.\c与c分隔符
\c与c只是一个代表,其中c可以换成任意一个字符
[root@localhost ~]# sed -i -r "\#/sbin/nologin#d" 4.txt
[root@localhost ~]# sed -i -r "\A/sbin/nologinAd" 4.txt
sed的常用指令
p :print,打印文本,通常与-n一起使用
d :delete,删除文本
a :append 表示追加文本,在指定行后添加一行或多行文本
c :用新文件修改(替换)当前行中的文本
[root@localhost ~]# sed "6c dfhtgfbtrthytyjyughm" 1.txt #修改第6行的内容为 dfhtgfbtrthytyjyughm
i :insert 在当前行之前插入文本,在指定行之前添加一行或多行文本
[root@localhost ~]# sed "2i dfhtgfbtrthytyjyughm" 1.txt
练习1 :
在/etc/passwd文件中1到3行之前插入HelloWorld
[root@localhost ~]# sed "1,3i HelloWorld" /etc/passwd
r: 从以外文件中读相关内容,写到相关行之后
[root@localhost ~]# sed "1,3r 5.txt" /etc/passwd #读取5.txt内容,写 到/etc/passwd的1-3行之后
w : 匹配到的行写入一个新的文件之中
[root@localhost ~]# sed "1,5w 6.txt" /etc/passwd #读取/etc/passwd中的1-5行写入到6.txt中
y:将字符转换成一个新的字符
[root@localhost ~]# sed "1,5y/sbin/1234/" /etc/passwd
sbin ---> 1234
s ---> 1
b ---> 2
i ---> 3
n ---> 4
s:用一个字符整体替换成另一个字符
[root@localhost ~]# sed "s/sbin/1234/" /etc/passwd
s指令替换对于行来说,是非贪婪性,如果需要全局替换则需要使用 g 指令
g :全局执行
[root@localhost ~]# sed "s/root/1234/g" /etc/passwd
i : 配合s指令配合一起使用时,则是忽略大小的作用
[root@localhost ~]# sed "s/ROOT/1234/i" /etc/passwd
练习
1:替换/etc/passwd中的root为ROOT
[root@localhost ~]# sed "s/root/ROOT/g" /etc/passwd
2:将模板机(192.168.15.200)中的ip替换成192.168.15.50
[root@localhost ~]# sed "s/\.200/\.50/g" /etc/sysconfig/network-scripts/ifcfg-eth[01]
3:删除/etc/passwd中的所有偶数行
[root@localhost ~]# sed "1~2d" /etc/passwd
[root@localhost ~]# sed "1~3d" /etc/passwd (奇数行)
4:在每一行的行首增加#号
[root@localhost ~]# sed "s/^ */#/g" 2.txt
将指定文本内的所有行前全部加上注释 sed -r 's/(.*)/#\1/g'
5:将Hello World替换成World Hello
[root@localhost ~]# sed -r "s/(Hello) (World)/\2 \1/g" 6.txt
6:将1.txt中的每一行都添加一个.bak的后缀
[root@localhost ~]# sed -r "s/(.*)/\1\.bak/g" 1.txt