Linux sed command detailed, very powerful

Source: Internet
Author: User
Tags printable characters

reprint: http://blog.chinaunix.net/u/22677/showart_1076318.html   1. IntroductionSED is a non-interactive editor. It does not modify the file unless you use Shell redirection to save the results. By default, all output lines are printed to the screen. The SED editor processes the file (or input) row by line and sends the results to the screen. The process is as follows: first SED saves the row currently being processed in a temporary buffer (also known as pattern space), and then processes the rows in the temporary buffers and sends the line to the screen when it is complete. Sed deletes a row from the temporary buffer after each processing, and then reads the next line in, processing, and displaying it. After the last line of the input file has been processed, sed finishes running. SED has a temporary buffer in each row and edits the copy, so the original file is not modified. 2. AddressingAddressing is used to decide which rows to edit. The address can be in the form of a number, a regular expression, or a combination of both. If no address is specified, SED processes all lines of the input file. The address is a number that represents the line number; a "$" symbol, which indicates the last row. For example:

sed -n ‘3p‘ datafile
只打印第三行

Displays only the file contents of the specified line range, for example:

# View only lines 100th through No. 200 of the file
Sed-n ' 100,200p ' Mysql_slow_query.log

Addresses are separated by commas, the address that needs to be processed is the range between the two lines, including both. Ranges can be represented by numbers, regular expressions, or a combination of both. For example:

sed ‘2,5d‘ datafile
#删除第二到第五行
sed ‘/My/,/You/d‘ datafile
#删除包含"My"的行到包含"You"的行之间的行
sed ‘/My/,10d‘ datafile
#删除包含"My"的行到第十行的内容

3. Commands and Options

The sed command tells SED how to handle each input line specified by the address, and all input lines are processed if no address is specified.

3.1 sed command

Command Function
A\

Adds one or more rows after the current line. Multiple rows, except for the last row, need "\" to continue at the end of each line

C\ Replaces the text in the current line with the new text after this symbol. Multiple rows, except for the last row, need "\" to continue at the end of each line
I\ Inserts text before the current line. Multiple rows, except for the last row, need "\" to continue at the end of each line
D Delete Row
H Copy the contents of the schema space to the staging buffer
H Append the contents of the schema space to the staging buffer
G Copy the contents of the staging buffer to the schema space, overwriting the original content
G Append the contents of the staging buffer to the schema space, appended to the original content
L List non-printable characters
P Print Line
N Read into the next input line and start processing it from the next command instead of the first command
Q End or exit sed
R Reading input rows from a file
! Apply commands to all rows except the selected row
S Replace one string with another
G Global substitution within a row
W Writes the selected line to a file
X Swap the contents of the staging buffer with the pattern space
Y Replace a character with another character (you cannot use the Y command on a regular expression)

3.2 sed options

Options Function
-E Multiple edits, which are used when multiple SED commands are applied to an input line
-N Cancel the default output
-F Specify the file name of the SED script
    4. Exit StatusSED does not like grep, regardless of whether the specified pattern is found, its exit status is 0. The exit status of SED is not 0 only if there is a syntax error in the command. 5. Regular expression meta-charactersLike grep, SED also supports special meta-characters for pattern lookups and substitutions. The difference is that the regular expression used by SED is the pattern enclosed by the slash line "/". If you want to change the regular expression delimiter "/" to another character, such as O, simply add a backslash before the character, followed by the regular expression after the character, followed by the character. Example: Sed-n ' \o^myop ' datafile
Metacharacters Function Example
^ Beginning of Line Locator /^my/matches all lines that begin with my
$ End-of-line locator /my$/matches all lines ending with my
. Match a single character other than the line break /M.. y/matches the line that contains the letter m, followed by two arbitrary characters, and then the letter Y
* Match 0 or more leading characters /my*/matches a line containing the letter m followed by 0 or more Y-letters
[] Matches any character within a specified character group /[mm]y/matching rows that contain my or my
[^] Matches any character that is not within the specified character group /[^mm]y/matches the line that contains Y, but before y, the character is not m or m
\(.. \) To save a matched character 1,20s/\ (you\) self/\1r/The pattern between the token metacharacters and saves it as a label 1, which can then be referenced by using \1. You can define up to 9 labels, numbering from the left, and the first one on the left. In this example, rows 1th through 20th are processed, you are saved as label 1, and if youself is found, it is replaced with your.
& Save the lookup string to reference in the replacement string s/my/**&**/Symbol & representative Find string. My will be replaced with **my**
\< Word Head Locator /\<my/matches lines that contain words that begin with my
\> Suffix Locator /my\>/matches a line that contains a word ending with my
X\{m\} Continuous m x /9\{5\}/matches a row containing 5 consecutive 9
X\{m,\} At least a m x /9\{5,\}/matches a row that contains at least 5 consecutive 9
X\{m,n\} At least m, but not more than n x /9\{5,7\}/matches rows that contain 5 to 7 consecutive 9
6. Example 6.1 P CommandThe command p is used to display the contents of the pattern space. By default, sed prints the input lines on the screen, and option-n is used to cancel the default print operation. When the Select-N and command p appear simultaneously, sed prints the selected content.

sed ‘/my/p‘ datafile
#默认情况下,sed把所有输入行都打印在标准输出上。如果某行匹配模式my,p命令将把该行另外打印一遍。


sed -n ‘/my/p‘ datafile
#选项-n取消sed默认的打印,p命令把匹配模式my的行打印一遍。

6.2 d Command

Command d is used to delete input rows. Sed first copies the input line from the file into the pattern space, then executes the SED command on the line, and finally displays the contents of the pattern space on the screen. If command d is emitted, the input lines in the current mode space are deleted and not displayed.

sed ‘$d‘ datafile
#删除最后一行,其余的都被显示

sed ‘/my/d‘ datafile
#删除包含my的行,其余的都被显示

6.3 S command

sed ‘s/^My/You/g‘ datafile
#命令末端的g表示在行内进行全局替换,也就是说如果某行出现多个My,所有的My都被替换为You。

sed -n ‘1,20s/My$/You/gp‘ datafile
#取消默认输出,处理1到20行里匹配以My结尾的行,把行内所有的My替换为You,并打印到屏幕上。

sed ‘s#My#Your#g‘ datafile
#紧跟在s命令后的字符就是查找串和替换串之间的分隔符。分隔符默认为正斜杠,但可以改变。无论什么字符(换行符、反斜线除外),只要紧跟s命令,就成了新的串分隔符。

6.4 E option

-E is an edit command that is used in cases where SED performs multiple editing tasks. All edit actions are applied to the line in the pattern buffer before the next line begins editing.

sed -e ‘1,10d‘ -e ‘s/My/Your/g‘ datafile

#选项-e用于进行多重编辑。第一重编辑删除第1-3行。第二重编辑将出现的所有My替换为Your。因为是逐行进行这两项编辑(即这两个命令都在模式空间的当前行上执行),所以编辑命令的顺序会影响结果。

6.5 r Command

The r command is a read command. SED uses this command to add the contents of a text file to a specific location in the current file.

sed ‘/My/r introduce.txt‘ datafile
#如果在文件datafile的某一行匹配到模式My,就在该行后读入文件introduce.txt的内容。如果出现My的行不止一行,则在出现My的各行后都读入introduce.txt文件的内容。

6.6 w Command

sed -n ‘/hrwang/w me.txt‘ datafile

6.7 a\ Command

The A\ command is an append command, appended with the addition of new text to the current line in the file (that is, the line in the read-in mode buffer). The appended line of text is placed below the SED command. If more than one row is to be appended, each row must end with a backslash, except for the last line. The last line ends with a quotation mark and a file name.

sed ‘/^hrwang/a\
>hrwang and mjfan are husband\
>and wife‘ datafile
#如果在datafile文件中发现匹配以hrwang开头的行,则在该行下面追加hrwang and mjfan are husband and wife

6.8 i\ Command

The I\ command inserts a new text in front of the current line.

6.9 c\ Command

SED uses this command to modify an existing text to a new text.

6.10 N Command

SED uses this command to get the next line of input files and read them into the pattern buffer, and any sed command will be applied to the next line immediately following the matching line.

sed ‘/hrwang/{n;s/My/Your/;}‘ datafile

Note: If you need to use multiple commands, or if you need to nest addresses in an address range, you must enclose the command in curly braces, write only one command per line, or use semicolons to split multiple commands in the same row. 6.11 y commandThis command is similar to the TR command in Unix/linux, where characters are converted from left to right in a one-to-one fashion. For example, y/abc/abc/will convert all lowercase a to a, lowercase B to B, and lowercase C to c.

sed ‘1,20y/hrwang12/HRWANG^$/‘ datafile
#将1到20行内,所有的小写hrwang转换成大写,将1转换成^,将2转换成$。
#正则表达式元字符对y命令不起作用。与s命令的分隔符一样,斜线可以被替换成其它的字符。

6.12 q command

The Q command will cause the SED program to exit without further processing.

sed ‘/hrwang/{s/hrwang/HRWANG/;q;}‘ datafile

6.13 h command and G command

#cat datafile

My name is hrwang.

Your name is mjfan.

hrwang is mjfan‘s husband.

mjfan is hrwang‘s wife.

 

sed -e ‘/hrwang/h‘ -e ‘$G‘ datafile

sed -e ‘/hrwang/H‘ -e ‘$G‘ datafile

#通过上面两条命令,你会发现h会把原来暂存缓冲区的内容清除,只保存最近一次执行h时保存进去的模式空间的内容。而H命令则把每次匹配hrwnag的行都追加保存在暂存缓冲区。

sed -e ‘/hrwang/H‘ -e ‘$g‘ datafile

sed -e ‘/hrwang/H‘ -e ‘$G‘ datafile

#通过上面两条命令,你会发现g把暂存缓冲区中的内容替换掉了模式空间中当前行的内容,此处即替换了最后一行。而G命令则把暂存缓冲区的内容追加到了模式空间的当前行后。此处即追加到了末尾。

7. Sed scripts

The SED script is a list of SED commands written in the file. The script requires no extra space or text at the end of the command. If you have multiple commands in a row, separate them with semicolons. When executing a script, sed first copies the first line of the input file to the pattern buffer and then executes all the commands in the script. After each line has been processed, SED copies the next line of the file to the pattern buffer and executes all the commands in the script. When using sed scripts, no quotation marks are used to ensure that the SED command is not interpreted by the shell. For example, sed scripts script:

#handle datafile
3i\
~~~~~~~~~~~~~~~~~~~~~
3,$s/\(hrwang\) is
 \(mjfan\)/\2 is \1/
$a\
We will love eachother forever!!
 

#sed -f script datafile
My name is hrwang
Your name is mjfan
~~~~~~~~~~~~~~~~~~~~~
mjfan is hrwang‘s husband.          #啦啦~~~
mjfan is hrwang‘s wife.
We will love eachother forever!!

Linux sed command detailed, very powerful

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.