sed工具

简介:

sed 的常用选项

选项 说明

-n 使用安静模式,在一般情况所有的 STDIN 都会输出到屏幕上,加入-n 后只打印被 sed 特殊处理的行

-e 多重编辑,且命令顺序会影响结果

-f 指定一个 sed 脚本文件到命令行执行,

-r Sed 使用扩展正则

-i 直接修改文档读取的内容,不在屏幕上输出

 

Sed 相对于grep擅长替换。

sed -n '5'p test.txt  //过滤出第五行 -n 取消默认输出

 sed -n '1,5'p test.txt  //过滤出指定的15

 sed -n '1,$'p test.txt  //过滤出1到尾行

 sed -n '/root/'p test.txt //过滤出含有root

sed -nr "/o+t/p" test.txt  //-r egrep类似的选项。

sed -nr "/root|nologin/p" test.txt  //支持和grep一样的选项

sed -nr "/o{2}/p" test.txt

 sed -n '/^1/'p test.txt

 sed -n 'in$'p test.txt

 sed -n '/r..o/'p test.txt

 sed -n '/oo*/'p test.txt

 sed -e '1'p -e '/111/'p -n test.txt  //-e多重编辑。打印第一行和匹配111字符(如果编辑的内容在同一行,一行会被打印两层。说明sed是流式编辑器)

[root@lsx-02 sed]# sed -n -e '1'p -e '/root/'p passwd  不仅打印指定的第一行还要打印出包含字符串root的行

root:x:0:0:root:/root:/bin/bash

root:x:0:0:root:/root:/bin/bash

 

[root@lsx-02 sed]# sed -n '/var/'IP passwd  区分大小写用I

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/Var/spool/lpd:/sbin/nologin

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

 

案例:比如一个比较大的日志文件,可能有几个G,日期可能今年年初到现在半年时间了,想前五个月的全删除掉,只留最后一个月的。

1. Vim效率低。

2. 可以使用sed

首先先确定好多少行。比如上个月的时间(时间戳)日期匹配出来。使用grep -n可以看多少行

 

sed '1'd test.txt  //删除第一行

 sed '1,3'd test.txt  //删除第一到第三行

 sed '/oot/'d test.txt  //删除含有oot字符的行

 sed '1,2s/ot/to/g' test.txt  //12行的ot换成to全局替换

 sed 's#ot#to#g' test.txt  //把全部ot替换成to

sed -r 's/oo+/aaa/g' test.txt  //查找到oo替换成aaa//前两斜杠之间可以使用正则表达式

 sed 's/[0-9]//g' test.txt  //删除数字

 sed 's/[a-zA-Z]//g' test.txt  // //删除英文字符

 sed -r 's/(rot)(.*)(bash)/\3\2\1/' test.txt  //第一段和最后一段调换位置

 sed 's/^.*$/123&/' test.txt=====sed -r 's/(.*)/lsx:&/g' test.txt

 sed -i 's/ot/to/g' test.txt  //把全部ot字符换成to  -i会改变文件内容

sed 's/\/root/lsx/' test.txt  //这种情况要么使用替换分隔符#或者at符号  

sed 's#lsx#/root#' test.txt

 

-r  表示不用转义

 

[root@lsx-02 sed]# cp passwd passwd.bak

[root@lsx-02 sed]# sed -i '1,20'd passwd.bak   i删除原文件内容 可以是数字也可以是一串字符所在的行

[root@lsx-02 sed]# wc -l passwd.bak

7 passwd.bak

 

[root@lsx-02 sed]# sed '1,10s/root/toor/g' passwd 类似vim替换

 

[root@lsx-02 sed]# sed -r '1,10s/ro+/r/g' passwd  用特殊字符+  要加-r

rt:x:0:0:rt:/rt:/bin/bash

bin:x:1:1:bin:/bin:/SBin/nologin

 

[root@lsx-02 sed]# head -n3 passwd | sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/'  头尾调换位置。第一段非冒号的字符一个或者多个:分隔符;第二段.*贪婪匹配到最后一个:;第三段非冒号的一个或者多个。一个小括号就是一个部分。

/bin/bash:x:0:0:root:/root:root

/SBin/nologin:x:1:1:bin:/bin:bin

/sbin/nologin:x:2:2:daemon:/sbin:daemon

 

[root@lsx-02 sed]# head passwd |sed 's/[a-zA-Z]//g'   //删除英文字符

::0:0::/://

::1:1::/://

::2:2::/://

 

sed -r 's/(.*)/lsx:&/g' test.txt

[root@lsx-02 sed]# head passwd | sed -r 's/(.*)/aaa:\1/'   在行的前面加aaa:()对应1 \1可以用&代替

aaa:root:x:0:0:root:/root:/bin/bash

aaa:bin:x:1:1:bin:/bin:/SBin/nologin

aaa:daemon:x:2:2:daemon:/sbin:/sbin/nologin




本文转自 虾米的春天 51CTO博客,原文链接:http://blog.51cto.com/lsxme/1983728,如需转载请自行联系原作者

相关文章
|
Linux Perl
linux高级命令行文本处理cut,sed,awk
cut   sort排序 wc  sed linux sed的详细指令 -i会删除 删除 替换 AWK awk命令详解   ...
1753 0
|
Linux 网络安全 Perl
Linux的文本处理工具浅谈-awk sed grep
Linux的文本处理工具浅谈 awk   老大 【功能说明】 用于文本处理的语言(取行,过滤),支持正则 NR代表行数,$n取某一列,$NF最后一列 NR==20,NR==30 从20行到30行 FS竖着切,列的分隔符 RS横着切,行的分隔符 ...
1402 0
|
Shell Perl 自然语言处理
|
Perl 存储 网络安全
|
Perl Linux 机器学习/深度学习
|
Perl 机器学习/深度学习

热门文章

最新文章