shell sed实例详解

系统 Linux
本文通过实例的形式详细讲述了shell sed的知识,帮助大家更加深入的了解shell sed。

一、sed (Stream Editor)

1、定位行:

sed -n '12,~3p' pass #从第12行开始,直到下一个3的倍数行(12-15行)

sed -n '12,+4p' pass #从第12行开始,连续4行(12-16行)

sed -n '12~3p' pass #从第12行开始,间隔3行输出一次(12,15,18,21...)

sed -n '10,$p' pass #从第10行至结尾

sed -n '4!p' pass #除去第4行

2、正则:'/正则式/'

sed -n '/root/p' /etc/passwd

sed -n '/^root/p' /etc/passwd

sed -n '/bash$/p' /etc/passwd

sed -n '/ro.t/p' /etc/passwd

sed -n '/ro*/p' /etc/passwd

sed -n '/[ABC]/p' /etc/passwd

sed -n '/[A-Z]/p' /etc/passwd

sed -n '/[^ABC]/p' /etc/passwd

sed -n '/^[^ABC]/p' /etc/passwd

sed -n '/\<root/p' /etc/passwd

sed -n '/root\>/p' /etc/passwd

3、扩展正则:

sed -n '/root\|yerik/p' /etc/passwd #拓展正则需要转义

sed -nr '/root|yerik/p' /etc/passwd #加-r参数支持拓展正则

sed -nr '/ro(ot|ye)rik/p' /etc/passwd #匹配rootrik和royerik单词

sed -nr '/ro?t/p' /etc/passwd #?匹配0-1次前导字符

sed -nr '/ro+t/p' /etc/passwd #匹配1-n次前导字符

sed -nr '/ro{2}t/p' /etc/passwd #匹配2次前导字符

sed -nr '/ro{2,}t/p' /etc/passwd #匹配多于2次前导字符

sed -nr '/ro{2,4}t/p' /etc/passwd #匹配2-4次前导字符

sed -nr '/(root)*/p' /etc/passwd #匹配0-n次前导单词

4、sed编辑(对行的插入、删除、替换操作)

sed '/root/a admin' /etc/passwd #在root行后追加一个admin行

sed '/root/i admin' /etc/passwd #在root行前插入一个admin

sed '/root/c admin' /etc/passwd #将root行替换为admin

sed '/root/d' /etc/passwd #删除含有root的行

s替换

sed -n 's/root/admin/p' /etc/passwd

sed -n 's/root/admin/2p' /etc/passwd #在每行的第2个root作替换

sed -n 's/root/admin/gp' /etc/passwd

sed -n '1,10 s/root/admin/gp' /etc/passwd

sed -n 's/root/AAA&BBB/2p' /etc/passwd #将root替换成AAArootBBB,&作反向引用,代替前面的匹配项

sed -ne 's/root/AAA&BBB/' -ne 's/bash/AAA&BBB/p' /etc/passwd #-e将多个命令连接起来,将root或bash行作替换

sed -n 's/root/AAA&BBB/;s/bash/AAA&BBB/p' /etc/passwd #与上命令功能相同

sed -nr 's/(root)(.*)(bash)/\3\2\1/p' /etc/passwd #将root与bash位置替换,两标记替换
或sed -n 's/\(root\)\(.*\)\(bash\)/\3\2\1/p' /etc/passwd

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

y替换

echo "sorry"|sed 'y/ory/ABC/' #一一对应替换(sABBC)

6、sed的模式空间和保持空间

h:模式---->保持

H:模式--->>保持

x:模式<--->保持

g:保持---->模式

G:保持--->>模式

例如:

111

222

333

444

# sed '1h;2,3H;4G'

分析

CMD 模式 保持

111 111 \n

1h 111 111

----------->111

222 222 111

2,3H 222 111\n222

----------->222

333 333 111\n222

2,3H 333 111\n222\n333

----------->333

444 444 111\n222\n333

4G 444\n111\n222\n333

----------->444\n111\n222\n333

1-10

11-22

22-33

11-22

34-END

7、sed特殊用法

sed -n '/root/w a.txt' #将匹配行输出到文件

sed '/root/r abc.txt' /etc/passwd #把abc.txt的文件内容读入到root匹配行后

sed -n '/root/w a.txt'

sed -n '/root/{=;p}' /etc/passwd #打印行号和匹配root的行

sed -n '/root/{n;d}' /etc/passwd #将匹配root行的下一行删除

sed -n '/root/{N;d}' /etc/passwd #将匹配root行和下一行都删除

sed '22{h;d};23,33{H;d};44G' pass

8、sed 脚本编写方法

<1>从文件读入命令

sed -f sed.sh

sed.sh文件内容:

s/root/yerik/p

s/bash/csh/p

<2>直接运行脚本 ./sed.sh /etc/passwd

#!/bib/sed -f

s/root/yerik/p

s/bash/csh/p

###################################

二、Sed练习

1,删除文件每行的第一个字符。

sed -n 's/^.//gp' /etc/passwd

sed -nr 's/(.)(.*)/\2/p' /etc/passwd

2,删除文件每行的第二个字符。

sed -nr 's/(.)(.)(.*)/\1\3/p' /etc/passwd

3,删除文件每行的最后一个字符。

sed -nr 's/.$//p' /etc/passwd

sed -nr 's/(.*)(.)/\1/p' /etc/passwd

4,删除文件每行的倒数第二个字符。

sed -nr 's/(.*)(.)(.)/\1\3/p' /etc/passwd

5,删除文件每行的第二个单词。

sed -nr 's/([^a-Z]*)([a-Z]+)([^a-Z]+)([a-Z]+)(.*)/\1\2\3\5/p' /etc/passwd

6,删除文件每行的倒数第二个单词。

sed -nr 's/(.*)([^a-Z]+)([a-Z]+)([^a-Z]+)([a-Z]+)([^a-Z]*)/\1\2\4\5\6/p' /etc/samba/smb.conf

7,删除文件每行的最后一个单词。

sed -nr 's/(.*)([^a-Z]+)([a-Z]+)([^a-Z]*)/\1\2\4/p' /etc/samba/smb.conf

8,交换每行的第一个字符和第二个字符。

sed -nr 's/(.)(.)(.*)/\2\1\3/p' /etc/passwd

9,交换每行的第一个单词和第二个单词。

sed -nr 's/([^a-Z]*)([a-Z]+)([^a-Z]+)([a-Z]+)(.*)/\1\4\3\2\5/p' /etc/samba/smb.conf

10,交换每行的第一个单词和最后一个单词。

sed -nr 's/([^a-Z]*)([a-Z]+)([^a-Z]+)([a-Z]+)(.*)/\1\4\3\2\5/p' /etc/passwd

11,删除一个文件中所有的数字。

sed 's/[0-9]*//g' /etc/passwd

12,删除每行开头的所有空格。

sed -n 's/^\ *//p' /etc/samba/smb.conf

sed -nr 's/( *)(.*)/\2/p' testp

13,用制表符替换文件中出现的所有空格。

sed -n 's/\ /\t/gp' pass

14,把所有大写字母用括号()括起来。

sed -nr 's/([A-Z])/(&)/gp' testp

sed -n 's/[A-Z]/(&)/gp' testp

15,打印每行3次。

sed 'p;p' pass

16,隔行删除。

sed -n '1~2p' pass

17,把文件从第22行到第33行复制到第44行后面。

sed '1,21h;22h;23,33H;44G' pass

18,把文件从第22行到第33行移动到第44行后面。

sed '22{h;d};23,33{H;d};44G' pass

19,只显示每行的第一个单词。

sed -nr 's/([^a-Z]*)([a-Z]+)([^a-Z]+)(.*)/\2/p' /etc/passwd

20,打印每行的第一个单词和第三个单词。

sed -nr 's/([^a-Z]*)([a-Z]+)([^a-Z]+)([a-Z]+)([^a-Z]+)([a-Z]+)(.*)/\2--\4/p' /etc/passwd

21,将格式为 mm/yy/dd 的日期格式换成 mm;yy;dd

date +%m/%Y/%d |sed -n 's#/#;#gp'

22, 逆向输出

cat a.txt

ABC

DEF

XYZ

输出样式变成

XYZ

DEF

ABC

原文链接:http://blog.uouo123.com/post/191.html

责任编辑:牛小雨 来源: blog.uouo123
相关推荐

2019-09-12 08:32:40

Linuxsed命令语法

2019-08-14 08:03:49

LinuxShell脚本web服务

2009-07-09 15:05:45

Servlet实例

2020-11-16 11:10:00

ShellLinux正则表达式

2014-08-06 09:57:15

hadoop ShelShell命令

2019-08-09 13:50:08

shellLinux

2017-06-27 14:15:22

LinuxShellsed

2009-06-11 08:59:35

2010-04-26 13:36:33

Unix Shell

2010-06-22 10:28:04

linux at命令

2009-06-10 14:53:25

netbeans st实例

2011-06-24 14:34:17

Qt 小票 打印

2022-12-02 09:02:36

Swift代码异步

2011-03-09 09:11:52

java反射机制

2013-12-02 09:49:59

2010-06-03 17:08:28

Hadoop Shel

2021-05-11 07:50:31

BashShell脚本

2009-07-15 13:41:00

JDBC实例

2009-09-07 07:38:05

Myeclipse项目

2011-02-28 13:04:27

RelativeLayAndroid Wid
点赞
收藏

51CTO技术栈公众号