Original http://blog.chinaunix.net/uid-24426415-id-77244.html
-------------------------------------------------------------------------------
Beginner's entry
the SED tool is a non-interactive stream editor. By default, only the output is affected and the input is not changed . An SED is a unit of behavior when processing a document. Features are: Delete, find replace, add , insert, read from other files.
In fact, these functions seem to be implemented with vim and other editors. So, why do we have sed?
main application Scenarios
- Too large a text
- Regular text changes
Command format for SED
...]
Delete
#删除第一行sed ‘1d‘ file
Note that this only affects the output stream. If you want to save it,
-i ‘1d‘ filename
Or
#输出到新文件sed ‘1d‘>newfilename
where the 1d command, we call 1 address, here refers to the first line.
Delete First line to last row
sed ‘1,$d‘ filename
Delete a line that contains a pattern
‘/pattern/d‘ filename#例如sed ‘/^$/d‘ filename
Find Replacements
#普通替换 将每行的第一个line替换成LINEsed ‘s/line/LINE/‘ filename
Sed ' s/line/line/[number]
Represents the maximum number of lines to replace in this line, and if number is G, replace all
Character Conversions
现在还没有见过重要的用法
Insert Text
#在第二行前插入一行sed ‘2 i insert_context‘ filename#在第二行之后插入一行sed ‘2 a insert_context‘ filename#在匹配的行之前插入一行sed ‘/pattern/i\new_word‘ filename
Print
#只打印出第一行 ,不加n的话会默认输出每一行sed -n ‘1p‘ filename#只打印出被修改的一行sed -n ‘s/the/THE/p‘ filename
Usage of the Linux sed command