文本三剑客

文本三剑客
sed awk grep

grep就是查找文本当中的内容,扩展正则表达式。

sed
sed是一种流编辑器,一次处理一行内容。
如果只是展示,会放到缓冲区(模式空间),展示结束之后,会从模式空间把操作结果删除
一行一行处理,当前行处理结束,才会处理下一行,知道文件末尾。
sed的命令格式和操作选项
sed -e '操作选项 ’ 文件1 文件2
-e 表示可以跟多个操作符,只有一个操作符,-e 可以省略。 表示指定操作。
sed -e ‘操作符1;操作符2’ 文件1 文件2
选项 :
-e:用于执行多个操作命令
-f: 在脚本中定义好了操作符,然后根据脚本内容的操作符对文件进行操作
-i:直接修改目标文件(慎用)
-n:仅显示script处理后的结果(不加-n,sed会有两个输出结果,加了-n之后就会吧默认输出屏蔽,只显示一个结果)

操作符:

p :打印结果
r :使用扩展正则表达式
s :替换,替换字符串
c :替换,替换行
y :替换,替换单个字符,多个字符替换必须和替换内容的字符长度保持一致
d :删除,删除行
a :增加,指定行的下面一行插入内容
$a:在最后一行插入新的内容
i :增加,在指定行的上面一行插入内容
$i : 在倒数第二行插入新的内容
r: 在行后增加文本内容
$r:读取其他文件的内容,然后插入到对象文件的最后一行

打印功能:

[root@test3 ssed]# #寻址打印
[root@test3 ssed]# sed -n '4p' test.txt 
e
[root@test3 ssed]# cat -n test.txt | sed -n '4p'
     4	e
[root@test3 ssed]# #打印最后一行
[root@test3 ssed]# sed -n '$p' test.txt 
[root@test3 ssed]# #行号范围打印
[root@test3 ssed]# sed -n '2,4p' test.txt   打印第二行到第四行
[root@test3 ssed]# sed -n '2,$p' test.txt     打印第2行到最后一行
[root@test3 ssed]# sed -n '2p;$p' test.txt    单独打印第二行和最后一行
[root@test3 ssed]# #打印奇数行和偶数行
[root@test3 ssed]# sed -n 'p;n'  test.txt #打印奇数行
[root@test3 ssed]# sed -n 'n;p'  test.txt  #打印偶数行
[root@test3 ssed]# sed -n '/o/p' test.txt  #过滤并打印包含o的行
[root@test3 ssed]# sed -n  -e '/a/p' -e '/o/p' test.txt    
[root@test3 ssed]# #使用正则表达式对文本内容进行过滤 
[root@test3 ssed]# sed -n '/^root/p' /etc/passwd    #打印以root开头的行
[root@test3 ssed]# sed -n '/bash$/p' /etc/passwd    #打印bash结尾的行
[root@test3 ssed]# sed -n '3,/bash$/p' /etc/passwd    #打印从第3行开始到以bash结尾的行

sed 使用扩展正则必须加r
[root@test3 ssed]# sed -rn '/(99:){2,}/p' /etc/passwd 
nobody:x:99:99:Nobody:/:/sbin/nologin

[root@test3 ssed]# #要么以root为开头,要么以bash结尾的行
[root@test3 ssed]# sed -rne '/^root|bash$/p'  /etc/passwd

[root@test3 ssed]# #sed的删除操作
[root@test3 ssed]# sed -n '3d;p' test.txt  #删除第三行打印其他的行
[root@test3 ssed]# sed -n '3d' test.txt    #删除第三行不打印其他行 

[root@test3 ssed]# sed -n '4d;6d;p' test.txt   #只删除第四行和第六行
[root@test3 ssed]# sed -n '4,6!d;p' test.txt   #删除第4行到第6行之外的行

[root@test3 ssed]# #匹配字符串删除行
[root@test3 ssed]# sed '/e/d' test.txt  # 删除包含e的行

[root@test3 ssed]# sed '/qqq/,/rrr/d' test.txt   # 删除包含qqq和rrr的行
[root@test3 ssed]# sed '/qqq/!d' test.txt      #删除除qqq以外的全部行

[root@test3 ssed]# #免交互 删除空行
[root@test3 ssed]# grep -v "^$" test.txt 
[root@test3 ssed]# cat test.txt  | tr -s "\n"
[root@test3 ssed]# sed '/^$/d' test.txt 


[root@test3 ssed]# #sed 替换 s c y
[root@test3 ssed]# #s 替换字符串
[root@test3 ssed]# sed -n 's/root/test/p' /etc/passwd    #将该文件中的每一行的第一个root替换为test
test:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/test:/sbin/nologin
[root@test3 ssed]# sed -n 's/root/test/gp' /etc/passwd    #将文件中的全部root字符替换成test
test:x:0:0:test:/test:/bin/bash
operator:x:11:0:operator:/test:/sbin/nologin
[root@test3 ssed]# sed -n 's/root/test/2p' /etc/passwd     #将该文件中的每一行的第二个root替换为test

[root@test3 ssed]# sed -n 's/^/#/p' test.txt    #文件内容全部注释
[root@test3 ssed]# sed -ne '4s/^/#/p' -e '6s/^/#/p'  test.txt  #注释第四行和第六行

[root@test3 ssed]# #替换字母大小写
[root@test3 ssed]# sed 's/[a-z]/\u&/'   test.txt     #首字母小写换大写
    u& 转换首字母换成大写的特殊模符号,\转义符。
[root@test3 ssed]# sed 's/[a-z]/\u&/g'   test.txt      #小写全部转化为大写


[root@test3 ssed]# sed 's/[A-Z]/\l&/' test.txt     #首字母大写换小写
[root@test3 ssed]# sed 's/[A-Z]/\l&/g' test.txt    #大写全部转化为小写
     l& 转换首字母换成小写的特殊模符号,\转义符。
 1135  #整行替换  c
 1136  sed '/qqq/c 6' test.txt 

[root@test3 ssed]# # y 单字符替换  多个字符替换必须和替换内容的字符长度保 持一致
[root@test3 ssed]# sed 'y/qqq/666/' test.txt 

[root@test3 ssed]# #  r  在行后增加文本内容 
[root@test3 ssed]# sed '/www/a  niupi666 ' test.txt    # a  在行下新增
[root@test3 ssed]# sed '/www/i  niupi666 ' test.txt    # i  在行上新增
[root@test3 ssed]# sed '/www/r  test1 ' test.txt       # r  读取文件新增到test.txt最后一行

[root@test3 ssed]# #使用sed对字符串和字符的位置进行互换。改变原字符的位置
[root@test3 ssed]# echo faguopaoche | sed -r 's/(faguo)(pao)(che)/\3\1\2/'  #分组之后字符串位置交换
chefaguopao
[root@test3 ssed]# echo 改变字符 |  sed -r 's/(.)(.)(.)(.)/\4\3\2\1/'
符字变改

sed -n 's/.*Version: \([0-9.]*\).*/\1/p' file.txt


sed 的主要作用:对文本的内容进行增删改查,强大的功能是改和增加

面试:

如何免交互删除文本内容,不删除文件

[root@test3 ssed]# #如何免交互删除文本内容
[root@test3 ssed]# sed -i 'd' test.txt      #清空文件内容不删除文件
[root@test3 ssed]# cat /dev/null > test.txt 
[root@test3 ssed]# echo " " > test.txt      # 假删除


[root@test3 ssed]# #免交互 删除空行
[root@test3 ssed]# grep -v "^$" test.txt 
[root@test3 ssed]# cat test.txt  | tr -s "\n"
[root@test3 ssed]# sed '/^$/d' test.txt 

笔试:
[root@test3 ssed]cat test1.txt | sed -r 's/(.*)-(.*)(\.jar)/\2/' 从文件中识别版本号
[root@test3 ssed]# sed -r 's/.*-([0-9.]+)\.jar/\1/'   test1.txt 



[root@test3 ssed]# tail -f /var/log/messages |  sed   -n  '/Jun 21 11:04:28/,/Jun 21 13:01:01/p'   #打印指定时间日志
  • 22
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码要你命

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值