sed——附加命令

1. 追加 a 命令

### 语法
$ sed '[address] a the-line-to-append' input-file
 

# 在指定行后添加一行,第2行后添加一行203

[root@tyler temp]# sed '2 a 203,Jack,Engineer' employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

203,Jack,Engineer

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

[root@tyler temp]#

# 在匹配行后添加多行

[root@tyler temp]# sed '/Jason/a 203,Jack Johnson,Engineer\n204,Mark Smith,Sales Engineer' employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

203,Jack Johnson,Engineer

204,Mark Smith,Sales Engineer

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

[root@tyler temp]#

 

 

2. 插入 i 命令

### 语法
$ sed  '[address] i the-line-to-insert'  input-file

# 在指定行前插入一行,在第2行之前添加一行203

[root@tyler temp]# sed '2 i 203,Jack,Engineer' employee.txt

101,John Doe,CEO

203,Jack,Engineer

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

[root@tyler temp]#

# 插入多行,用法同上

 

3. 修改 c 命令

### 语法
$ sed '[address] c the-line-to-insert' input-file
# 用新行取代旧行,可以与 s 命令等价使用

[root@tyler temp]# sed '2 c 202,Jack,Engineer' employee.txt

[root@tyler temp]# sed '2s/.*/202,Jack,Engineer/' employee.txt

 

# 可以用多行取代一行,用法同上

 

4. 打印不可见字符 l 命令

[root@tyler temp]# sed -n 'l' tmp.txt

hndisjdljdisj$

nmamlka$

doiskdois$

dsdlkpsldp$

[root@tyler temp]# sed -n 'l' dos.txt

hndisjdljdisj\r$

nmamlka\r$

doiskdois\r$

dsdlkpsldp\r$

[root@tyler temp]#

 

### l命令后指定数字,会在第n个字符使用一个不可见的自动折行

[root@tyler temp]# sed -n 'l 20' employee.txt

101,John Doe,CEO$

102,Jason Smith,IT \

Manager$

103,Raj Reddy,Sysad\

min$

104,Anand Ram,Devel\

oper$

105,Jane Miller,Sal\

es Manager$

 

 

5. 打印行号 = 命令

### 打印所有行的行号
[root@tyler temp]# sed '=' employee.txt

1

101,John Doe,CEO

2

102,Jason Smith,IT Manager

3

103,Raj Reddy,Sysadmin

4

104,Anand Ram,Developer

5

105,Jane Miller,Sales Manager

### 打印指定行的行号

[root@tyler temp]# sed '1,3=' employee.txt

1

101,John Doe,CEO

2

102,Jason Smith,IT Manager

3

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

[root@tyler temp]#


### 打印匹配到行的行号

[root@tyler temp]# sed '/Jane/=' employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

5

105,Jane Miller,Sales Manager


### 配合-n,打印总行数(最后一行的行号)

[root@tyler temp]# sed -n '$=' employee.txt

5

[root@tyler temp]#

 

6. 转换字符 y 命令

### 命令y可以根据对应位置转换字符

# 常用于,大小写转换

[root@tyler temp]# sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' employee.txt

101,JOHN DOE,CEO

102,JASON SMITH,IT MANAGER

103,RAJ REDDY,SYSADMIN

104,ANAND RAM,DEVELOPER

105,JANE MILLER,SALES MANAGER

[root@tyler temp]#

 

7. 操作多个文件

[root@tyler temp]# sed -n '/root/p' /etc/passwd /etc/group

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

operator:x:11:0:operator:/root:/sbin/nologin

dockerroot:x:496:491:Docker User:/var/lib/docker:/sbin/nologin

root:x:0:

dockerroot:x:491:

[root@tyler temp]#

 

8. sed退出 q 命令

### 在指定行,退出

[root@tyler temp]# sed '3 q' employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

### 匹配到行,退出

[root@tyler temp]# sed '/Manager/q' employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

[root@tyler temp]#

 

9. 读取文件数据 r 命令

### r命令会读取另一个文件的内容,并在指定的位置打印出来
# 在指定到的行,打印另一个文件内容

[root@tyler temp]# sed '$rlog.txt' employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

log: input.txt

log:

log: testing resumed

log:

log:output created


# 在匹配到的行,打印另一个文件的内容

[root@tyler temp]# sed '/Raj/r log.txt' employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

log: input.txt

log:

log: testing resumed

log:

log:output created

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

[root@tyler temp]#

 

10.   选项命令

### -l选项用于指定行的长度(需要配合l命令使用)
[root@tyler temp]# sed -nl 20 'l' employee.txt

101,John Doe,CEO$

102,Jason Smith,IT \

Manager$

103,Raj Reddy,Sysad\

min$

104,Anand Ram,Devel\

oper$

105,Jane Miller,Sal\

es Manager$

### l命令也可以单独使用指定行长度

[root@tyler temp]# sed -n 'l20' employee.txt

101,John Doe,CEO$

102,Jason Smith,IT \

Manager$

103,Raj Reddy,Sysad\

min$

104,Anand Ram,Devel\

oper$

105,Jane Miller,Sal\

es Manager$
[root@tyler temp]#
 

### 容易混淆的还有

# -i 选项,直接修改原文件 <> i命令用于插入数据

# -n 选项,屏蔽默认输出 <> n命令打印模式空间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值