码迷,mamicode.com
首页 > 系统相关 > 详细

Linux基础命令(四)

时间:2020-09-17 18:36:35      阅读:40      评论:0      收藏:0      [点我收藏+]

标签:ddr   一起   删除   shell   sshd   add   linux基础命   use   流编辑器   

grep,我们可以使用grep命令在文本中查找指定的字符串
参数:-i 不区分大小写,默认是区分的,-v显示不匹配的行,-n显示匹配行及行号 -c只统计匹配的行数,-E使用扩展的egrep -w精确匹配单词 -o只输出匹配的内容 -Ax -Bx -Cx
grep -Ei "linux | shell" test.txt 不区分大小写,同时过滤包含linux和shell的字符串
grep -v "^$|#" test.txt 过滤掉注释和空行

sed 字符流编辑器
sed是操作,过滤,转换文本内容的强大工具,常用功能包括对文件实现快速增删改查。
参数:-n取消默认的sed输出,常用sed内置命令p连用 -i直接修改文件内容,而不是输出到终端,如果不使用- i 则sed只是修改内存中的数据,并不会影响磁盘上的文件
sed常用内置命令字符:a在指定行后添加一行或多行文本 d删除匹配行的文本,i在指定行前插入文本 p打印匹配行的内容,通常p和-n一起使用
[root@localhost dir]# cat >user.txt<<EOF

101,ZHANGSAN,CEO
102,lisi,coo
103,wangwu,cio
104,zhaoliu,cto
105,peter,wto
EOF
[root@localhost dir]# sed ‘2a 106,alex,AAA‘ user.txt
101,ZHANGSAN,CEO
102,lisi,coo
106,alex,AAA
103,wangwu,cio
104,zhaoliu,cto
105,peter,wto
[root@localhost dir]# sed ‘2i 107,cici,BBB‘ user.txt
101,ZHANGSAN,CEO
107,cici,BBB
102,lisi,coo
103,wangwu,cio
104,zhaoliu,cto
105,peter,wto
[root@localhost dir]# sed ‘3a 108,danis,CCC\n109,linonghao,DDD‘ user.txt
101,ZHANGSAN,CEO
102,lisi,coo
103,wangwu,cio
108,danis,CCC
109,linonghao,DDD
104,zhaoliu,cto
105,peter,wto
[root@localhost dir]# sed ‘2d‘ user.txt
101,ZHANGSAN,CEO
103,wangwu,cio
104,zhaoliu,cto
105,peter,wto
[root@localhost dir]# sed ‘2,4d‘ user.txt
101,ZHANGSAN,CEO
105,peter,wto
[root@localhost dir]# sed ‘s#ZHANGSAN#zhangsan#g‘ user.txt
101,zhangsan,CEO
102,lisi,coo
103,wangwu,cio
104,zhaoliu,cto
105,peter,wto
[root@localhost dir]# sed -n ‘2p‘ user.txt
102,lisi,coo
[root@localhost dir]# sed -n ‘2,4p‘ user.txt
102,lisi,coo
103,wangwu,cio
104,zhaoliu,cto
sed命令默认操作的是内存中的数据,如果想要真正修改文件内容,就需要使用- i选项将修改写到磁盘上。
[root@localhost dir]# sed -n ‘13,17p‘ /etc/ssh/sshd_config
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
[root@localhost dir]# cat 1.txt
1
one
2
two
3
three
[root@localhost dir]# sed ‘N;s#\n#=#g‘ 1.txt
1 =one
2=two
3=three

Linux基础命令(四)

标签:ddr   一起   删除   shell   sshd   add   linux基础命   use   流编辑器   

原文地址:https://blog.51cto.com/90856/2530129

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!