文本处理工具-sed

文本处理工具

sed介绍

sed用法

sed中带有变量,需要把’‘变成"",或者变量两端用三个单引号括起来’’’$RANDOM’’’

语法

  • sed [option]… ‘script’ inputfile…

’script’脚本

script = 地址命令

地址定界

  • 不给地址:对全文进行处理
  • 单地址:

#: 指定的行, $: 最后一行
/pattern/:被此处模式所能够匹配到的每一行

  • 地址范围:

#,#
#,+#
/pat1/,/pat2/
#,/pat1/

  • ~:步进

1~2 奇数行
2~2 偶数行

option常用选项

  • -p 打地址内
  • -n 不显示除地址外的内容
  • -r 扩展正则表达式
  • -e 多次处理
  • -f 指定文件,文件内容是sed脚本,直接调用
  • -d 删除
  • -i i.bak 修改并备份,后缀为.bak,也可以用其他后缀,-i.back,-i.bak2

显示第二行

[root@centos7 root2018-07-31]#ifconfig ens33 |sed '2p'
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.32.132  netmask 255.255.255.0  broadcast 192.168.32.255
        inet 192.168.32.132  netmask 255.255.255.0  broadcast 192.168.32.255
        inet6 fe80::6fe1:3752:6965:4d10  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:26:05:98  txqueuelen 1000  (Ethernet)
        RX packets 127907  bytes 13596929 (12.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 115502  bytes 96072101 (91.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@centos7 root2018-07-31]#ifconfig ens33 |sed -n '2p'
        inet 192.168.32.132  netmask 255.255.255.0  broadcast 192.168.32.255

正则表达式

[root@centos7 root2018-07-31]#cat /etc/passwd | sed -n '/^tom/p'
tomcat:x:1006:1008::/home/tomcat:/bin/bash
tom:x:2000:2000:'My name is tom!':/home/tom:/bin/bash
tommy:x:2009:2011::/home/tommy:/bin/bash
tommy1:x:2010:2012::/home/tommy1:/bin/bash

奇数,偶数为间隔来取行

n~M,从n行开始间隔为M来取行

[root@centos7 root2018-07-31]#sed -n '1~2p' f1
1
3
5
7
[root@centos7 root2018-07-31]#sed -n '1~3p' f1
1
4
7
[root@centos7 root2018-07-31]#sed -n '2~2p' f1
2
4
6
8

多点编辑

每一个-e后处理一个脚本内容

[root@centos7 root2018-07-31]#sed -n -e '2p'  -e '1~5p' f1
1
2
6

编辑命令

  • a[\]txt append,显示在行后追加\后边的txt文本内容,但没有修改文件,\可以用可不用,加入-i.bak之后,修改文件,并备份文件
  • i[\]txt insert,在符合条件的行之前插入txt内容
  • c[\]txt 代替符合条件的行,即删除符合条件的行,然后其位置添加txt文本,替换一正行
  • w /path/file 符合条件的行保存至/path/file中
  • r /path/file 把文件内容追加到符合条件的行后边
  • = 为模式空间中的行打印行号
  • ! 模式空间中匹配行取反处理

a命令选项测试

[root@centos7 root2018-07-31]#sed '1,3aThis is a test' f1
1
This is a test
2
This is a test
3
This is a test
[root@centos7 root2018-07-31]#cat f1
1
2
3

i命令选项测试

[root@centos7 root2018-07-31]#sed '1,3i\   Can You Love Me ?' f1
   Can You Love Me ?
1
   Can You Love Me ?
2
   Can You Love Me ?
3

c命令选项测试

[root@centos7 root2018-07-31]#sed '1,2c\   Can You Love Me ?' f1
   Can You Love Me ?
3

-i.bak测试

[root@centos7 root2018-07-31]#cat f1
1
2
3
[root@centos7 root2018-07-31]#sed -i.bak '1,3aThis is a test' f1
[root@centos7 root2018-07-31]#cat f1
1
This is a test
2
This is a test
3
This is a test
[root@centos7 root2018-07-31]#ls
f1  f1.bak 

w命令选项测试

[root@centos7 root2018-07-31]#sed '1,2w/data/root2018-07-31/f4' f1
1
2
3
[root@centos7 root2018-07-31]#cat f4
1
2

sed工具

搜索替换

  • 跟vim的搜索替代用法一样
  • s/txt1/txt2/g s///可以换成s@@@或s###,txt2替换txt1
  • -r 支持正则表达式
  • 后向引用中s@aaaaaa@&@,&表示第一个表达式的所有字符
[root@centos7 root2018-07-31]# ifconfig ens33 | sed -n '2p' | sed -r 's@inet (.*)[[:space:]]*net.*@\1@'
        192.168.32.132  
[root@centos7 root2018-07-31]# ifconfig ens33 | sed -r '2!d; s@inet (.*)[[:space:]]*net.*@\1@'
        192.168.32.132
cat /etc/httpd/conf/httpd.conf | sed -r '/^#<VirtualHost/,/^#<\/VirualHost>/s@#@@g'

sed高级用法

  • P:打印模式空间开端至\n内容,并追加到默认输出之前
  • h: 把模式空间中的内容覆盖至保持空间中
  • H:把模式空间中的内容追加至保持空间中
  • g: 从保持空间取出数据覆盖至模式空间
  • G:从保持空间取出内容追加至模式空间
  • x: 把模式空间中的内容与保持空间中的内容进行互换
  • n: 读取匹配到的行的下一行覆盖至模式空间
  • N:读取匹配到的行的下一行追加至模式空间
  • d: 删除模式空间中的行
  • D:如果模式空间包含换行符,则删除直到第一个换行符的模式空间中的文本,并不会读取新的输入行,而使用合成的模式空间重新启动循环。如果模式空间不包含换行符,则会像发出d命令那样启动正常的新循环 (删除行首到第一个回车,若是最后一行,不删除)
sed -n 'n;p' FILE
sed '1!G;h;$!d' FILE
sed 'N;D‘ FILE
sed '$!N;$!D' FILE
sed '$!d' FILE
sed ‘G’ FILE
sed ‘g’ FILE
sed ‘/^$/d;G’ FILE
sed 'n;d' FILE
sed -n '1!G;h;$p' FILE
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值