余苏明的幻想乡

Chapter 4. Sed Execution(执行)

23. Multiple Sed Commands in Command Line

在命令行上多个sed命令

1. Use multiple -e option in the command line

使用多个-e选项

1
sed -e 'command1' -e 'command2' -e 'command3'

Search for root, or nobody, or mail in the /etc/passwd file:

1
sed -n -e '/^root/ p' -e '/^nobody/ p' -e '/^mail/ p' /etc/passwd

2. Break-up several sed commands using \

使用\分割多个sed命令
当你有一个非常长的命令时,你可以使用\换行分割多个选项

1
2
3
4
sed -n -e '/^root/ p' \
-e '/^nobody/ p' \
-e '/^mail/ p' \
/etc/passwd

3. Group multiple commands using { }

当你有一个大量的sed 选项时,你可以使用{}

1
2
3
4
5
sed -n '{
/^root/ p
/^nobody/ p
/^mail/ p
}' /etc/passwd

tips:-e选项后面每个选项都用''分割,{}里面每一行一个选项,不用''分割

24. Sed Script Files(sed脚本文件)

如果你想去重复使用一套sed命令时,你可以创建一个sed脚本文件,每一行一个sed选项,使用-f参数调用文件

1
2
3
$ vi mycommands.sed s/\([^,]*\),\([^,]*\),\(.*\).*/\2,\1,\3/g s/^.*/<&>/
s/Developer/IT Manager/
s/Manager/Director/

1
2
3
4
5
6
$ sed -f mycommands.sed employee.txt
<John Doe,101,CEO>
<Jason Smith,102,IT Director>
<Raj Reddy,103,Sysadmin>
<Anand Ram,104,IT Director>
<Jane Miller,105,Sales Director>

25. Sed Comments(sed注释)

我们知道sed使用晦涩难懂的语言,可能这次知道怎么什么意思,下次就忘了,所以可以使用注释行,帮助快速回忆

1
2
3
4
5
6
7
8
9
$ vi mycommands.sed
# Swap field 1 (employee id) with field 2 (employee name)
s/\([^,]*\),\([^,]*\),\(.*\).*/\2,\1,\3/g
# Enclose the whole line within < and >
s/^.*/<&>/
# Replace Developer with IT Manager
s/Developer/IT Manager/
# Replace Manager with Director
s/Manager/Director/

Note: If the 1st 2 characters of the 1st line in the*.sed script are#n, sed will automatically use the-n (don’t print the pattern buffer)
option.
如果*.sed脚本第一行的前两个字符时#n,则sed将自动使用-n参数(不打印缓冲区模式)

26. Sed as an Interpreter

sed作为一种编译器
和shell脚本一样,可以在sed脚本第一行添加编译环境#!/bin/sed -f使bash识别为sed脚本。

1
2
3
4
5
6
7
8
9
10
$ vi myscript.sed
#!/bin/sed -f
# Swap field 1 (employee id) with field 2 (employee name)
s/\([^,]*\),\([^,]*\),\(.*\).*/\2,\1,\3/g
# Enclose the whole line within < and >
s/^.*/<&>/
# Replace Developer with IT Manager
s/Developer/IT Manager/
# Replace Manager with Director
s/Manager/Director/

现在,给sed脚本执行权限,并执行

1
2
3
chmod u+x myscript.sed
./myscript.sed employee.txt

你也可以在第一行后面使用-n 参数

1
2
3
4
$ vi testscript.sed
#!/bin/sed -nf
/root/ p
/nobody/ p

不是-fn参数

27. Modifying the Input File Directly(直接修改输入文件)

使用-i参数,直接修改输入文件

Replace John with Johnny in the original employee.txt file itself:

1
2
3
4
5
6
7
8
$ sed -i 's/John/Johnny/' employee.txt
$ cat employee.txt
101,Johnny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

你可以直接使用-i参数修改输入文件,但是要异常小心。
这时候,你可以在-i参数后面添加命名,在写入新内容之前创建一个原始文件的备份

Replace John with Johnny in the original employee.txt file but save a backup copy:

1
$ sed -ibak 's/John/Johnny/' employee.txt

已经将原始文件备份为employee.txtbak

1
2
3
4
5
6
$ cat employee.txtbak
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

而原始文件已经修改了

1
2
3
4
5
6
$ cat employee.txt
101,Johnny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

你也可以使用长的-i参数--in-place.下面两个命令是等价的

1
2
sed -ibak 's/John/Johnny/' employee.txt
sed --in-place=bak 's/John/Johnny/' employee.txt