the shell sed usage

A: sed text editor

sed: Powerful streaming text editor

Supplement

sed is a stream editor, it is in the very text processing tool that perfectly fit a regular expression to use, extraordinary.Handling, storing the row currently being processed in a temporary buffer, called a "model space" (pattern space), followed by treatment with the contents of the buffer sed command processing is complete, the contents of the buffer sent to the screen. Then the next line, which is repeated until the end of the file. File contents not changed , unless you use redirection to store the output. Sed is mainly used to automatically edit one or more documents; simplify repeated operation of the document; write conversion procedures.

sed command format

sed [options] 'command' file(s)
sed [options] -f scriptfile file(s) 

Options

-e<script>或--expression=<script>:以选项中的指定的script来处理输入的文本文件;
-f<script文件>或--file=<script文件>:以选项中指定的script文件来处理输入的文本文件; 
-h或--help:显示帮助; 
-n或--quiet或——silent:仅显示script处理后的结果;-V或--version:显示版本信息。

parameter

文件:指定待处理的文本文件列表。

sed command Action Description

**a\** 在当前行下面添加文本。
**i\** 在当前行上面插入文本。 
**c\** 把选定的行改为新的文本。 
**d** 删除,删除选择的行。 
**D** 删除模板块的第一行。 
**s** 替换指定字符 
**p** 打印模板块的行。
**P** (大写) 打印模板块的第一行
**w file** 写并追加模板块到file末尾。 
**W file** 写并追加模板块的第一行到file末尾。 
**h** 拷贝模板块的内容到内存中的缓冲区。 
**H** 追加模板块的内容到内存中的缓冲区。 
**g** 获得内存缓冲区的内容,并替代当前模板块中的文本。 
**G** 获得内存缓冲区的内容,并追加到当前模板块文本的后面。 
**l** 列表不能打印字符的清单。 
**n** 读取下一个输入行,用下一个命令处理新的行而不是用第一个命令。 
**N** 追加下一个输入行到模板块后面并在二者间嵌入一个新行,改变当前行号码。  
**q** 退出Sed。 **b lable** 分支到脚本中带有标记的地方,如果分支不存在则分支到脚本的末尾。 
**r file** 从file中读行。 
**t label** if分支,从最后一行开始,条件一旦满足或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾。 
**T label** 错误分支,从最后一行开始,一旦发生错误或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾。 
**!** 表示后面的命令对所有没有被选定的行发生作用。**=** 打印当前行号码。 
**#** 把注释扩展到下一个换行符以前。 

sed substitution tags

**g** 表示行内全面替换。 
**p** 表示打印行。 
**w** 表示把行写入一个文件。 
**x** 表示互换模板块中的文本和缓冲区中的文本。 
**y** 表示把一个字符翻译为另外的字符(但是不用于正则表达式) 
**\1** 子串匹配标记 
**&** 已匹配字符串标记 

sed yuan Character Set

**^** 匹配行开始,如:/^sed/匹配所有以sed开头的行。 
**$** 匹配行结束,如:/sed$/匹配所有以sed结尾的行。 
**.** 匹配一个非换行符的任意字符,如:/s.d/匹配s后接一个任意字符,最后是d。 
**** * 匹配0个或多个字符,如:/*sed/匹配所有模板是一个或多个空格后紧跟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开头的单词的行。 
**\>** 匹配单词的结束,如/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的行。 

p (the section line printing mode) operation mode:

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture DescriptionHere Insert Picture Description
D (delete, delete selected row) operation mode
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
a (add text below the current line) operation
Here Insert Picture Descriptioni (insert text in the line above the current) operation mode

Here Insert Picture DescriptionHere Insert Picture Description c (the selected line to the new text) operation mode
Here Insert Picture Description
w file (written and appended to the end of the template block file) operation mode

Here Insert Picture DescriptionOther uses sed screenshot:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture DescriptionHere Insert Picture Description
Here Insert Picture Description
Here Insert Picture DescriptionHere Insert Picture Description
Here Insert Picture Description
Here Insert Picture DescriptionHere Insert Picture Description
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/bmengmeng/article/details/91982584