linux菜鸟入门之文本处理小工具sed

   

文本处理

为了更好的处理文本必须掌握一些文本处理的小工具,这样才可以更高效的工作,文本处理3剑客grep,sed,awk。grep很简单,没有什么好说的了,sed和awk就比较难了,我刚开始学的时候根本不理解为什么要学sed和awk,学这个有什么意义。后来才发现,其实这很重要,比如我想写一个监控磁盘的脚本,那么一下子就想到了free这个命令了。于是运行free,发现有些是我想要的,有些却不是我想要的参数,比如,swap我就不想看到,我只想看到总共有多少,用了多少,还剩多少,这就得看awk的了,awk的功能十分强大,是以后脚本的好帮手。


先了解一下sed吧

     sed是Strem Editor 的缩写。就是流编辑器的缩写。是一个行编辑器。是逐行编译的。还有vim(全屏编辑器)。

但他默认是不编辑原文的,当然加一个 -i 选项就可以编辑原文了。

sed ‘Adress Command’ file ...

1./pattern1/,/pattern2/

第一次被pattern1匹配的行开始,到被pattern2匹配的行结束。

2.LineNumber

指定的行($表示最后一行)

3.command

        d  删除

        s  替换

        g  全文

        p  打印

做一个小例子吧

[root@localhost test_xuexi]# cat test
a
b
abc
aaab
aaaabcd

[root@localhost test_xuexi]# sed '1,2d' test 
abc
aaab
aaaabcd

[root@localhost test_xuexi]# sed '/a..b/d' test 
a
b
abc

[root@localhost test_xuexi]# sed '1,+2d' test 
aaab
aaaabcd

[root@localhost test_xuexi]# 
-n 只打印符合条件的行(因为sed会打印出符合条件的行和原全文)

a \”string“: 在指定的行后面追加一行,内容是“string”

i  \"string"; 在指定的行前面添加一行,内容是”string“

r FILE : 将制定的文件的内添加至符合条件的行后面

[root@localhost test_xuexi]# sed '2r /etc/issue' /etc/fstab

#
\S
Kernel \r on an \m

# /etc/fstab
# Created by anaconda on Tue Sep 20 07:56:24 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=f35003eb-898a-4681-8342-f8f6de03e541 /                       xfs     defaults        0 0
UUID=e19602a8-407c-414d-b38f-83f14718477a /boot                   xfs     defaults        0 0
UUID=cfd32983-7e1e-4d7d-ae3e-1e38dfc17616 swap                    swap    defaults        0 0

w FILE ;将符合条件的行保存至某个文件 。这个文件是放到了/tmp 下的。


s/pattern/string/g    查找并替换字符  g代表全文 i表示忽略大小写。

[root@localhost tmp]# tail -n 3 /etc/passwd
zxx:x:1001:1001:zxx,2+2lib,029-1910,029-1910:/home/zzx:/bin/bash
liuy:x:1002:1003::/home/liuy:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@localhost tmp]# tail -n 3 /etc/passwd|sed 's/\/bin\/bash/\/bin\/sh/g'
zxx:x:1001:1001:zxx,2+2lib,029-1910,029-1910:/home/zzx:/bin/sh
liuy:x:1002:1003::/home/liuy:/bin/sh
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

这里完全可以使用正则表达式

[root@localhost test_xuexi]# cat test 
a
b
abc
aaab
aaaabcd

[root@localhost test_xuexi]# sed 's/\(abc\)/\1def/g'
^C
[root@localhost test_xuexi]# sed 's/\(abc\)/\1def/g' test 
a
b
abcdef
aaab
aaaabcdefd
[root@localhost test_xuexi]# cat test 
My love
[root@localhost test_xuexi]# sed 's/l\(..e\)/L\1/g' test 
My Love

-e 选项:sed 支持多个操作

-f 选项:支持脚本 ,命令在脚本里面。

-r选项;  选择扩展的正则表达式

再做一个小练习吧,将history的命令输出值的空白全删了

[root@localhost test_xuexi]# history |sed 's/ //g'
264rmlines
265clear
266ls
取出一个文件路径的目录名称。

[root@localhost test_xuexi]# pwd
/root/test_xuexi
[root@localhost test_xuexi]# pwd|sed -r 's#^(/.*/)[^/]*#\1#g'
/root/
[root@localhost test_xuexi]# 








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值