d
删除
w
write 写入到文件
a
append 在某一行 跟随某一个
[jerry]$ sed '4 a 7) Adultry, Paulo Coelho, 234' books.txt
得到的结果:
1) A Storm of Swords, George R. R. Martin, 1216
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
7) Adultry, Paulo Coelho, 234
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864
c
change
[jerry]$ sed '/The Alchemist/ c 3) Adultry, Paulo Coelho, 324' books.txt
变成
1) A Storm of Swords, George R. R. Martin, 1216
2) The Two Towers, J. R. R. Tolkien, 352
3) Adultry, Paulo Coelho, 324
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864
i
insert
[jerry]$ sed '4 i 7) Adultry, Paulo Coelho, 324' books.txt
变成
1) A Storm of Swords, George R. R. Martin, 1216
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
7) Adultry, Paulo Coelho, 324
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864
Translate
[address1[,address2]]y/list-1/list-2/
[jerry]$ echo "1 5 15 20" | sed 'y/151520/IVXVXX/'
变成
I V IV XX
l
[address1[,address2]]l
或者
[address1[,address2]]l [len]
显示 非打印字符
SED uses the l command to display hidden characters in the text. For example, tab character with \t and End-Of-Line with $ character
如果制定了 l 25 ,l 之后的数字表示 25个字符之后 wrapping after a certain number of characters
A wrap limit of 0 means never break the line unless there is a new line character. The following simple command illustrates this.
[jerry]$ sed -n 'l 0' books.txt
On executing the above code, you get the following result:
1) A Storm of Swords,George R. R. Martin,1216$
2) The Two Towers,J. R. R. Tolkien,352$
3) The Alchemist,Paulo Coelho,197$
4) The Fellowship of the Ring,J. R. R. Tolkien,432$
5) The Pilgrimage,Paulo Coelho,288$
6) A Game of Thrones,George R. R. Martin,864$
q
quit
[jerry]$ sed '3 q' books.txt
1) A Storm of Swords, George R. R. Martin, 1216
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
In addition to this, SED can also accept a value which can be used as the exit status. The following command shows its exit status as 100.
[jerry]$ sed ‘/The Alchemist/ q 100’ books.txt
On executing the above code, you get the following result:
1) A Storm of Swords, George R. R. Martin, 1216
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
Now let us verify the exit status.
[jerry]
echo
?
On executing the above code, you get the following result:
100
r
读命令
[jerry]$ sed ‘3 r junk.txt’ books.txt
On executing the above code, you get the following result:
1) A Storm of Swords, George R. R. Martin, 1216
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
==This is junk text.==
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864
e
[jerry]$ sed ‘3 e date’ books.txt
在第三行执行的是unix 的date 命令
[jerry]$ echo -e "date\ncal\nuname" > commands.txt
[jerry]$ cat commands.txt
On executing the above code, you get the following result:
date
cal
uname
Commands from the file are self-explanatory. In the absence of command after e, SED executes all these commands one by one. The following simple example illustrates this.
[jerry]$ sed 'e' commands.txt
On executing the above code, you get the following result:
Sun Sep 7 18:14:20 IST 2014
September 2014
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Linux
Like other SED commands, the execute command also accepts all valid ranges of addresses.
n
next Line
N
将下一行 添加进来 并且是append 方式
P
从 p 类似不过是打印到 newline 标志位置
[jerry]$ sed -n ‘N;P’ books.txt
只打印出技术行
v
检查sed 版本
In the following example, the SED version is greater than version 4.2.2, hence the SED command aborts its execution.
[jerry]$ sed ‘v 4.2.3’ books.txt
On executing the above code, you get the following result:
sed: -e expression #1, char 7: expected newer version of sed
But if the provided version is lesser than or equal to version 4.2.2, then the command works as expected.
[jerry]$ sed ‘v 4.2.2’ books.txt
On executing the above code, you get the following result: