余苏明的幻想乡

Chapter 5. Additional Sed Commands(扩展的sed命令)

28. Append Line After (a command)

你可以使用a命令在匹配到行后面添加新行

Syntax(语法)

1
$ sed '[address] a the-line-to-append' input-file

Add a new record(记录)to the employee.txt file after line number:

employee.txt文件在指定行号后面添加一个新的记录

1
2
3
4
5
6
7
$ sed '2 a 203,Jack Johnson,Engineer' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
203,Jack Johnson,Engineer
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

29. Insert Line Before (i command)

使用i命令在匹配到行前面插入新行

Syntax(语法)

1
$ sed '[address] i the-line-to-insert' input-file

30. Change Line (c command)

使用c命令,修改匹配到的行

Syntax(语法)

1
$ sed '[address] c the-line-to-insert' input-file

31. Combine a, i, and c Commands(联合使用a,i,c命令)

1
2
3
4
5
6
7
8
9
10
11
12
13
$ sed '/Jason/ { a\
204,Jack Johnson,Engineer i\
202,Mark Smith,Sales Engineer c\
203,Joe Mason,Sysadmin
}' employee.txt
101,John Doe,CEO
202,Mark Smith,Sales Engineer
203,Joe Mason,Sysadmin
204,Jack Johnson,Engineer
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

32. Print Hidden Characters (l command)(显示隐藏字符)

例如\t tab字符,$行尾

1
2
3
4
$ cat tabfile.txt
fname First Name
lname Last Name
mname Middle Name

执行l命令显示tabEOL

1
2
3
4
$ sed -n l tabfile.txt
fname\tFirst Name$
lname\tLast Name$
mname\tMiddle Name$

当你在l命令后面指定一个数字n,则在匹配行的第n个字符处打印一个\

This works only on GNU sed.

1
2
3
4
5
6
7
8
9
10
$ 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$

33. Print Line Numbers (= command)

打印行号

1
2
3
4
5
6
$ 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
1
2
3
4
5
6
7
$ 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

34. Change Case (using the y ‘transform’ command)

一对一替换,感觉用处不大。加密用?

In this example character “a” will be transformed to A, b to B, c to C, etc.:

1
2
3
4
5
6
$ sed 'y/abcde/ABCDE/' employee.txt
101,John DoE,CEO
102,JAson Smith,IT MAnAgEr
103,RAj REDDy,SysADmin
104,AnAnD RAm,DEvElopEr
105,JAnE MillEr,SAlEs MAnAgEr

35. Multiple Files in Command Line

可以在一行命令行下对多个文件使用同一个sed

1
2
3
$ sed -n '/root/ p' /etc/passwd /etc/group
root:x:0:0:root:/root:/bin/bash
root:x:0:

36. Quit Sed (q command)

q命令是第一次匹配到行后退出

Note:q命令对地址范围是无效的。因为匹配到第一行的时候就退出了

37. Read from File (r command)

读取文件,并将文本内容输出到标准输出

1
2
3
4
5
6
7
8
9
10
[root@localordb sed_test]# sed '1 r log.txt' employee.txt
101,John Doe,CEO
1.717 2.601
1.771 26.151
1.789 20.002
1.789 25.130
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,anand Ram,Developer
105,Jane Miller,Sales Manager

38. Simulating Unix commands in sed (cat, grep, head)

sed模仿其他命令

39. Sed Command Line Options

-n option

静默模式,禁止默认的输出
You can also use --quiet, or –-silent instead of -n. They are identical in function.
我们也可以使用--quiet, or –-silent代替-n

-f option(--file option)

读取sed脚本

-e option(--expression option)

执行一个sed命令

-i option(-ibak等价于--in-place=bak)

替换选项,重写输入文件

-c option(和-i选项联合使用)

-i联合使用,-ibak是备份原文件,加-c选项则是新建一个bak文件,将-i替换的文件写入新建文件。源文件不变
你也可以使用--copy选项。等价与-c

-l option(--line-length option)

指定行长度进行分割

40. Print Pattern Space (n command)注意,不是-n选项

读取下一行,并打印模式空间里的内容

1
2
3
4
5
6
7
8
9
10
11
12
sed '{
> =
> n
> }' employee.txt
1
101,John Doe,CEO
102,Jason Smith,IT Manager
3
103,Raj Reddy,Sysadmin
104,anand Ram,Developer
5