The use of Linux sed command

First of all, I apologize for the message sent last night. Although it's tricky everyone, I'm still very happy.


Sed is a stream editor, used in conjunction with regular expressions. When sed processes files, it saves the currently processed text in the buffer. The sed command processes the contents of the buffer and displays the contents of the buffer on the screen. Able to simplify some repetitive actions.


Let's first understand the command format of sed

sed [options] 'command' filesname

sed [options] -f scriptfilename filesname


sed command

a \ Insert text below the current line

i \ Insert text above the current line

c\ Change the selected line to new text

D Delete the first line of the template block

d delete the selected row

g Get the contents of the buffer and replace the text in the current template block

G. . . . . . . . , And append to. . . . . Behind

h Copy the contents of the template block to the buffer;

H Append the content of the template block to the buffer;

l The list of contents cannot be printed in the list;

n Read the next input line, use the next command to process the new line instead of the first command

N Append the next input line to the back of the template block and embed a new line between the two, changing the current line number

p print the lines of the template block

P print the first line of the template

w filename write and append the template block to the end of the file

W filename write and append the first line of the template block to the end of the file

! Indicates that the following commands have an effect on all rows that are not selected

= Print current number

# Extend the comment to before the next newline character.


Replacement mark

g means full replacement in the line.

p represents the printing line.

w means to write the line to a file.

x means swapping the text in the template block and the text in the buffer.

y means to translate one character into another character (but not used in regular expressions)

\1 Substring matching token

& Matched string tag

Metacharacter set

^ The beginning of the matching line, such as: /^sed/ matches all lines beginning with sed.

 $ Matches the end of the line, such as: /sed$/ matches all lines ending with sed. 

. Match any character that is not a newline character, such as: /sd/ matches s followed by any character, and finally d.

 * Match 0 or more characters, such as: /*sed/ matches all lines where the template is one or more spaces followed by sed.

 [] 匹配一个指定范围内的字符,如/[ss]ed/匹配sed和Sed。 

[^] 匹配一个不在指定范围内的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行。 

\(..\) 匹配子串,保存匹配的字符,如s/\(love\)able/\1rs,loveable被替换成lovers。 

& 保存搜索字符用来替换其他字符,如s/love/**&**/,love这成**love**。 

\< 匹配单词的开始,如:/\ 匹配单词的结束,如/love\>/匹配包含以love结尾的单词的行。 

x\{m\} 重复字符x,m次,如:/0\{5\}/匹配包含5个0的行。

 x\{m,\} 重复字符x,至少m次,如:/0\{5,\}/匹配至少有5个0的行。 

x\{m,n\} 重复字符x,至少m次,不多于n次,如:/0\{5,10\}/匹配5~10个0的行。


实例:

替换操作

替换文本中的字符串

sed 's/StringOriginal/NewString/' filename


-n -p 参数,只打印发生变化的行

sed -n 's/StringOriginal/NewString/p' filename


直接编辑文件选项-i,会匹配filename文件中每一行的第一个StringOringinal替换为NewString:

sed -i 's/StringOriginal/NewString/g' filename


全面替换标记

使用后缀 /g 标记会替换每一行中的所有匹配:

sed 's/StringOriginal/NewString'  filename


定界符

以上命令中字符 / 在sed中作为定界符使用,也可以使用任意的定界符: 

sed 's:StringFileOriginal:NewStringFile:g' 

sed 's|StringFileOriginal|NewString|g' 


定界符出现在样式内部时,需要进行转义: 

sed 's/\/bin/\/usr\/local\/bin/g'



读写文件,追加插入

sed '/String/r file' filename

file被读进来,显示在String匹配的行后面,如果匹配多行,则file的内容将显示在所有匹配行的下面。


在Example中,所有包含String的都写入file中

sed -n '/String/w file' Example


追加命令:a\

将 this is a test line 追加到 以test 开头的行后面

sed '/^test/a\this is a test line' file


在 test.conf 文件第2行之后插入 this is a test line: 

sed -i '2a\this is a test line' test.conf


插入命令:

将 this is a test line 追加到以test开头的行前面: 

sed '/^test/i\this is a test line' file 

在test.conf文件第5行之前插入this is a test line: 

sed -i '5i\this is a test line' test.conf



祝大家生活愉快!

哈哈图片图片图片图片图片图片图片


Guess you like

Origin blog.51cto.com/15080014/2642053