linux中sed带入参数,Linux sed 15个参数

本文详细介绍了sed命令的15个常用参数,包括从文件读入(r)、写入文件(w)、追加(a)、插入(i)、打印(n)、变形(y)、退出(q)、模式空间操作(h、G、x)以及删除(d)、替换(s)、多点编辑(e)和脚本文件(-f)。通过实例演示了这些参数如何在文本处理中发挥作用,帮助读者深入理解sed命令的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、15个参数

1、r 从文件读入

[root@watchout2 ~]# cat file

1

2

3

4

5

[root@watchout2 ~]# cat newfile

a

b

c

d

e

[root@watchout2 ~]# sed '/a/r file' newfile (读入文件file,并显示在newfile文件中匹配行a之后)

a

1

2

3

4

5

b

c

d

e

[root@watchout2 ~]# touch /file

[root@watchout2 ~]# echo "aaaaaaaaaaaaaaaaaaaaaaaaaa" > /file

[root@watchout2 ~]# sed '/a/r /file' newfile  (读入的文件在不同的路径)

a

aaaaaaaaaaaaaaaaaaaaaaaaaa

b

c

d

e

[root@watchout2 ~]#

2、w写入文件

[root@watchout2 ~]# sed '/a/w bo' newfile

a

b

c

d

e

[root@watchout2 ~]# cat bo ([root@watchout2 ~]# sed -n '/a/w bobo' newfile )

a

3、a 追加命令

[root@watchout2 ~]# sed '/b/a \bobo' newfile

a

b

bobo

c

d

e

4、i 插入

[root@watchout2 ~]# sed '/a/i \bobo' newfile

bobo

a

b

c

d

e

5、n 下一个

[root@watchout2 ~]# sed -n '/a/{n;p}' newfile(打印匹配行的下一行)

b

[root@watchout2 ~]# sed '/a/{n;s/b/c/p}' newfile

a

c

c

c

d

e

6、y 变形命令

[root@watchout2 ~]# sed '1,3y/abc/ABC/' newfile

A

B

C

d

e

y命令就是将小写转换成了大写,正则表达式元字符不能使用这个命令。

7、q 退出命令

[root@watchout2 ~]# sed '3q' newfile

a

b

c

打印前三行后退出。

8、h命令   是将pattern space 模式空间(临时缓冲区)的内容复制到holding buffer保持缓冲区

9、G命令 是将holding buffer中的内容取得,尔后放回pattern space中,且追加到相应行的末 尾

g命令是将holding buffer 中的内容取得,尔后放回pattern space 中,且替换相应的行

[root@watchout2 ~]# sed -e '/a/h' -e '/d/G' newfile

ab

c

d

a

e

h命令会把a匹配行,放入保持缓冲区,G命令会把保持缓冲区中的内容放入模式空间,并追加到匹配行d的下一行。

[root@watchout2 ~]# sed -e '/a/h' -e '$G' newfile

ab

c

d

e

a

与上相同,只是$代表最后一行。

[root@watchout2 ~]# sed -e '/a/h' -e '/b/g' newfile

aac

d

e

[root@watchout2 ~]# sed -e '/a/h' -e '$g' newfile

ab

c

d

a

以上h命令会把匹配行a,放入保持缓冲区,g命令会读取保持缓冲区的内容,将匹配行b(第二个例子就是$最后一行)替换为a 。注:a将覆盖匹配行b(第二个例子就是$最后一行)

10、x命令 是pattern space模式空间将被holding buffer保持缓冲区中的内容替换

[root@watchout2 ~]# sed -e '/a/h' -e '/d/x' newfile

a

b

c

a

e

匹配行d 将被匹配行a替换。

11、-n选项取消sed的默认行为,sed 默认行为是-p ,

root:/tmp>sed '/6/p' num   -p参数打印num的内容,尔后匹配“6”在次打印6,所以6会出现两次。

1

2

3

4

5

6

67

8

root:/tmp>sed -n '/6/p' num  -n选项取消sed的默认行为(-p ),所以只打印“6”

6

root:/tmp>

12、删除:d命令

删除第6行

root:/tmp>sed '6d' num

1

2

3

4

5

7

8

root:/tmp>

从第6行删除到行尾

root:/tmp>sed '6,$d' num

1

2

3

4

5

root:/tmp>

d删除最后一行

root:/tmp>sed '$d' num

1

2

3

4

5

6

7

root:/tmp>

d删除匹配行

root:/tmp>sed '/6/d' num

1

2

3

4

5

7

8

root:/tmp>

13、替换:s命令

s表示替换,g表示作用范围整个行,如果没有g标志则只有每行第一个被替换

root:/tmp>sed 's/1/8/g' num    (sed默认有-p参数打印搜索后的所有行。如g后面加p,那么匹配行就会打印两次)

8

2

3

4

5

6

7

8

root:/tmp>

行首“1”开头被替换为“8”

root:/tmp>sed 's/^1/8/g' num

8

2

3

4

5

6

7

8

root:/tmp>

&符号表示替换字符串中被找到的部分,所以每个数字后面被追加.6789

root:/tmp>sed 's/[0-9]/&.6789/g' num

1.6789

2.6789

3.6789

4.6789

5.6789

6.6789

7.6789

8.6789

root:/tmp>

\(..\) 保存匹配的字符到标签\1中。  s/\(5\)/\1.6789/  蓝色部分保存到标签\1中。从表达式最左边开始,向右最多可以使用9个标签

root:/tmp>sed 's/\(5\)/\1.6789/g' num

1

2

3

4

5.6789

6

7

8

root:/tmp>sed 's/\([0-9]\)/\1.6789/g' num

1.6789

2.6789

3.6789

4.6789

5.6789

6.6789

7.6789

8.6789

root:/tmp>

s后面的字符是分隔搜索字符串和替换字符串的分隔符。默认分隔符是斜杠,不论什么字符紧跟s命令都被认为是新的分隔符

root:/tmp>sed 's#6#shell#g' num

1

2

3

4

5

shell

7

8

root:/tmp>

14、多点编辑:e命令

root:/tmp>sed -e '1,6d' -e 's/8/8.shell/g' num

7

8.shell

root:/tmp>

15、-f  引导sed脚本文件名

16、选定行的范围:逗号

从第一行到第五行

root:/tmp>sed -n '1,5p' num

1

2

3

4

5

从第一行到第五行,然后在搜索

root:/tmp>sed '1,5s/3/3.linux/g' num

1

2

3.linux

4

5

6

7

8

显示从第三行到行首为6的中间所有行

root:/tmp>sed -n '3,/^6/p' num

3

4

5

6

root:/tmp>

匹配1和5后面追加“******Total********”

root:/tmp>sed '/1/,/5/s/$/******Total********/g' num

1******Total********

2******Total********

3******Total********

4******Total********

5******Total********

6

7

8

root:/tmp>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值