SED command detailed

Source: Internet
Author: User

SED command:

Stream Editor, line editors

Sed is a stream editor, which is a very useful tool in text processing and can be used perfectly in conjunction with regular expressions. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/9C/wKioL1ep46nzQ9qsAAB2Wf-5zkg405.png "title=" 10.png "alt=" Wkiol1ep46nzq9qsaab2wf-5zkg405.png "/>

syntax:sed [options] ..... ' Script ' [Input-file]

Script: Address delimitation Edit Command

Common options:

-N: Brake printing of content not in output mode space
-E: Multi-point editing
-f/path/to/sed_script_file: Reads the edit script from the specified file, one edit command per line;
-R: Supports the use of extended regular expressions
-I.: Edit the original file directly

Address definition:

1, not to address: The full text to deal with;

2. Single Address:

#: The specified # line

/pattern/: The line to which this pattern is matched;

3. Address range:

#,#: From line # to line #

#,+#: From line # to section #+#

#,/pat1/: line from # line match to first match to PAT1

$: Last line

4. Step in: ~

: All Odd lines

2~2: All even lines

Edit command:

-D: Delete
-P: Display Mode space content
A \text: Append text at the back of the line, enabling multiple-row append using \ n
I \text: Append text at the back of the line, enabling multiple-row append using \ n
C \text: Replace the matched line with the text specified here
W/path/to/somefile: Save the pattern space to match the line to the specified file
R/path/to/somefile: Reads the contents of the specified file to the current file after the line that the pattern matches to, and the file is merged.
= Print line numbers for the lines to which the pattern matches
! : Conditional inversion
s///: Find replacements whose delimiters can be specified by themselves. such as: @@@,###
Replace tag:

G: Global Substitution

W/path/to/somefile: Saves the result of the substitution success to the specified file;

P: Show the rows that were successfully replaced


Sed meta-characters:

^ The match line starts, such as:/^sed/matches all lines that begin with SED.
$ Matches the end of the line, such as:/sed$/matches all lines ending in sed.
. Any character that matches a non-line break, such as:/s.d/matches S followed by an arbitrary character, and finally D.
* Match 0 or more characters, such as:/*sed/match all the templates are one or more characters followed by the SED line.
[] Matches a specified range of characters, such as/[ss]ed/-match sed and sed.
[^]

Matches a character that is not within the specified range, such as:/[^a-rt-z]ed/matches the beginning of a letter that does not contain a-r and t-z, followed by the line of ED

\(.. \) Match substrings, save matching characters such as s/\ (love\) able/\1rs,loveable are replaced with lovers.
& Saving search characters is used to replace other characters, such as S/love/**&**/,love, which becomes **love**.
\< Matches the beginning of a word, such as:/\<love/matches a line that contains the beginning of love
\> Matches the end of a word, such as/love\>/, that matches a line containing a word ending in love.
X\{m\} Repeat characters x,m times, such as:/0\{5\}/matches rows that contain 5 0.
X\{m,\} Repeat the character X, at least m times, such as:/0\{5,\}/matches at least 5 rows of 0.
X\{m,n\} Repeat the character X, at least m times, not more than n times, such as:/0\{5,10\}/matches the line of 5~10 0.


Example:

1. Delete all whitespace characters from the beginning of lines in the/etc/grub2.conf file that begin with whitespace

[[Email protected] ~]# sed ' s/^[[:space:]]\+//g '/etc/grub2.cfg

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/85/9C/wKiom1ep2VSSeIDFAAAhPqLm-jA614.png "title=" 1.png " alt= "Wkiom1ep2vsseidfaaahpqlm-ja614.png"/>

2. Delete all # and white space characters at the beginning of the line beginning with #, followed by at least one white-space character, in the/etc/fstab file

[[Email protected] ~]# sed ' s/^#[[:space:]]\+//g '/etc/fstab

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/85/9B/wKioL1ep2j2xhacKAAAcT-uyPv0800.png "title=" 2.png " alt= "Wkiol1ep2j2xhackaaact-uypv0800.png"/>

3. Add # At the beginning of each line of/root/install.log

[[Email protected] ~]# sed ' s/^/#/g ' install.log

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/85/9B/wKioL1ep2qDSFWCgAAA34LK8UHo681.png "title=" 3.png " alt= "Wkiol1ep2qdsfwcgaaa34lk8uho681.png"/>

4. Add # to the beginning of the line in the/etc/fstab file that does not begin with #

[Email protected] ~]# sed-e ' s/^[^#]/#&/g '-e ' s/^$/#/'/etc/fstab

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/9C/wKiom1ep2snRtfaNAAAkySh5d8A515.png "title=" 4.png " alt= "Wkiom1ep2snrtfanaaakysh5d8a515.png"/>

There are two spaces in the SED command, preceded by the pattern space (pattern word), and one called hold space. Text that has been edited in the pattern space can be placed in the hold space. The content of space and pattern space can be exchanged, which greatly improves the flexibility of use. Using the preserve space requires the use of advanced editing commands:


Advanced Editing Commands:

h:
h:
g:
g:
x:
n:
n:
D:&NBSP;
d:


Example:

1. Sed-n ' N;p ' FILE

Parse: The edit command is divided into two parts:

①-n: Do not print content automatically in the mode space

② Edit command: N: Overwrites the next line of the read to the pattern space,

That is, the second row is read when the first row is started, and the second row in the pattern space is overwritten by the first line, that is, the second row of files is now in pattern space. Then p prints the lines in the pattern space. Then read the third, four-line file in the file. The fourth line in the pattern space is overwritten by the third row, and then p prints the fourth line, one analogy until the last row. The result of the output is 第二、四、六 ... And so on even lines.

Result: Even rows are displayed

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/85/9C/wKiom1ep2xGh-oKqAAARqwAaQ1Y530.png "title=" 5.png " alt= "Wkiom1ep2xgh-okqaaarqwaaq1y530.png"/>

2, sed ' 1! G;h;$!d ' FILE

Parse: The edit command is divided into three parts:

①:1! G: the G option is not executed on the first line, and content in the hold space is appended to the pattern space.

②:h: Overwrite content in pattern space to hold space

③:$!d: Delete Not last line

First, the command reads the first line of content in the file, ① does not execute, ② execution will overwrite the contents of the pattern space to preserve space, that is, the pattern space is now the first line of content, keeping space as the first line. ③ execution, the pattern space is empty, and the space is the first row.

Then read the second line of content, ① execution, pattern space for the second row, the first row. Keep Space constant. The ② executes, keeping the space changing to the second row, the first row. ③ execution, the pattern space becomes empty.

And so on, when the command reads to the last row, the ① mode space is the last line, the penultimate line, ... ② executes, keeping space in reverse order as the pattern space. ③ does not execute.

Result: Display the contents of a file in reverse order

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/9C/wKiom1ep2zChBzIPAAAMB9ZbF3Y837.png "title=" 6.png " alt= "Wkiom1ep2zchbzipaaamb9zbf3y837.png"/>

3. Sed ' $! n;$! D ' File

Parse: The edit command is divided into two parts:

①$! N: Not the last line will append the next line of the row to the pattern space.

②$! D: Deleting all rows in the multiline mode space is not the last line.

First read the first line of the file, ① execution, pattern space for the first row, the second row. ② execution, mode space is empty.

And so on, read to the second penultimate line of the file, ① execution, pattern space is the penultimate line, the penultimate line. ② does not execute.

Result: Remove the last two lines of the file.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/9C/wKiom1ep20rDfPbYAAAGQ1AJ0LM191.png "title=" 7.png " alt= "Wkiom1ep20rdfpbyaaagq1aj0lm191.png"/>

4, sed '/^$/d; G ' FILE:

Parse: The edit command is divided into two parts:

①/^$/D: The match to a blank line is deleted.

②g: Appends rows in the hold space to the pattern space.

① executes, the command matches the blank line in the file, and then deletes it. ② executes, keeps space empty, appends blank lines to the pattern space line.

Result: Delete blank lines in the file, and then add blank lines for all non-blank lines in the background

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/85/9B/wKioL1ep22aAoreDAAAazXWtIK8248.png "title=" 8.png " alt= "Wkiol1ep22aaoredaaaazxwtik8248.png"/>

5. Sed ' n,d ' FILE

Parsing: The editing command is divided into two parts;

①:n: Overwrites the next line of the row to the pattern space in the read match

②:D: Delete

First, the command reads the first line of the file, ① executes, the pattern space is the second row, ② executes, and the pattern space is empty. command does not have the-n option, the output mode space is automatically printed, that is, the first line of output,

And so on, the output mode space content automatic consent, that is 第一、三、五 ... Yes

Result: Odd rows are displayed

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/9C/wKiom1ep24Cx5TxSAAAIgmCjL1U596.png "title=" 9.png " alt= "Wkiom1ep24cx5txsaaaigmcjl1u596.png"/>

This article is from the "I ' m Groot" blog, so be sure to keep this source http://groot.blog.51cto.com/11448219/1836313

SED command detailed

Related Article

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.