Sed是一个流式编辑器,在Linux,Unix中用来修改文件,当你想用一个命令来自动的对文件做修改的时候,这个命令往往是个不错的选择。很多人以为这个命令只能是用作字符串的替换,
其实它还能做很多的事情。
####输入文件file.txt
$ cat file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.
####只替换每行的第一个匹配的字符串
####在这里讲一下's/unix/linux/', s 是 英文单词substitution的缩写,表示替换的意思, / 是分隔符号, unix是要查找的字符串的模式,linux是要替换成的字符串
####默认情况下,sed只替换每行匹配的第一个字符串
$ sed 's/unix/linux/' file.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
####只替换每行的第二个匹配的字符串,这里的2表示第二个匹配的字符串
$ sed 's/unix/linux/2' file.txt
unix is great os. linux is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.
####替换每行匹配的所有的字符串
$ sed 's/unix/linux/g' file.txt
linux is great os. linux is opensource. linux is free os.
learn operating system.
linuxlinux which one you choose.
####两个选择结合来使用
$ sed 's/unix/linux/3g' file.txt
linux is great os. linux is opensource. linux is free os.
learn operating system.
linuxlinux which one you choose.
####如果想把http://替换成www,因为字符串里面含有斜线/,就加入了反斜线
sed 's/http:\/\//www/' file.txt
这种写法看着比较麻烦,sed中可以采用不同的分隔符
sed 's_http://_www_' file.txt
sed 's|http://|www|' file.txt
####有的时候,需要在匹配的字符串后面加一些字符,就需要用到&,它表示匹配到的字符串
####给匹配到的字符串加上大括号{}
$ sed 's/unix/{&}/' file.txt
{unix} is great os. unix is opensource. unix is free os.
learn operating system.
{unix}linux which one you choose.
$
$ sed 's/unix/{&&}/' file.txt
{unixunix} is great os. unix is opensource. unix is free os.
learn operating system.
{unixunix}linux which one you choose.
$
####把unix变成unixunix
$ sed 's/\(unix\)/\1\1/' file.txt
unixunix is great os. unix is opensource. unix is free os.
learn operating system.
unixunixlinux which one you choose.
$
####把unixlinux变成linuxunix
$ sed 's/\(unix\)\(linux\)/\2\1/' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
linuxunix which one you choose.
$
####转置每行开头的三个字符
$ sed 's/^\(.\)\(.\)\(.\)/\3\2\1/' file.txt
inux is great os. unix is opensource. unix is free os.
aelrn operating system.
inuxlinux which one you choose.
$
####/p会duplicate输出
$ sed 's/unix/linux/p' file.txt
linux is great os. unix is opensource. unix is free os.
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
linuxlinux which one you choose.
$
####-n会supress duplicated lines
$ sed -n 's/unix/linux/p' file.txt
linux is great os. unix is opensource. unix is free os.
linuxlinux which one you choose.
$
####只是-n,no output
$ sed -n 's/unix/linux/' file.txt
$
####Running multiple sed commands.
$ sed 's/unix/linux/' file.txt| sed 's/os/system/'
linux is great system. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you chosysteme.
$
$ sed -e 's/unix/linux/' -e 's/os/system/' file.txt
linux is great system. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you chosysteme.
$
####Replacing string on a specific line number.
$ sed '3 s/unix/linux/' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
$
####Replacing string on a range of lines.
$ sed '1,3 s/unix/linux/' file.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
$
####Here $ indicates the last line in the file. So the sed command replaces the text from second line to last line in the file.
$ sed '2,$ s/unix/linux/' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
$
####Replace on a lines which matches a pattern.
####Here the sed command first looks for the lines which has the pattern "linux" and then replaces the word "unix" with "centos".
$ sed '/linux/ s/unix/centos/' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
centoslinux which one you choose.
$
####Deleting lines.
$ sed '2 d' file.txt
unix is great os. unix is opensource. unix is free os.
unixlinux which one you choose.
$
$ sed '5,$ d' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.
$
####Duplicating lines
####You can make the sed command to print each line of a file two times.
$ sed 'p' file.txt
unix is great os. unix is opensource. unix is free os.
unix is great os. unix is opensource. unix is free os.
learn operating system.
learn operating system.
unixlinux which one you choose.
unixlinux which one you choose.
$
####Sed as grep command
####You can make sed command to work as similar to grep command.
$ grep 'unix' file.txt
unix is great os. unix is opensource. unix is free os.
unixlinux which one you choose.
$
####Here the sed command looks for the pattern "unix" in each line of a file and prints those lines that has the pattern.
$ sed -n '/unix/p' file.txt
unix is great os. unix is opensource. unix is free os.
unixlinux which one you choose.
$
####You can also make the sed command to work as grep -v, just by using the reversing the sed with NOT (!).
####The ! here inverts the pattern match.
$ grep -v 'unix' file.txt
learn operating system.
$
$ sed -n '/unix/!p' file.txt
learn operating system.
$
####Add a line after a match.
####The sed command can add a new line after a pattern match is found. The "a" command to sed tells it to add a new line after a match is found.
$ sed '/unix/ a "Add a new line"' file.txt
unix is great os. unix is opensource. unix is free os.
"Add a new line"
learn operating system.
unixlinux which one you choose.
"Add a new line"
####Add a line before a match
####The sed command can add a new line before a pattern match is found. The "i" command to sed tells it to add a new line before a match is found.
$ sed '/unix/ i "Add a new line"' file.txt
"Add a new line"
unix is great os. unix is opensource. unix is free os.
learn operating system.
"Add a new line"
unixlinux which one you choose.
####Change a line
####The sed command can be used to replace an entire line with a new line. The "c" command to sed tells it to change the line.
$ sed '/unix/ c "Change line"' file.txt
"Change line"
learn operating system.
"Change line"
####Transform like tr command
####The sed command can be used to convert the lower case letters to upper case letters by using the transform "y" option.
####Here the sed command transforms the alphabets "ul" into their uppercase format "UL"
$ sed 'y/ul/UL/' file.txt
Unix is great os. Unix is opensoUrce. Unix is free os.
Learn operating system.
UnixLinUx which one yoU choose.
$