Linux Basics detailed sed command

Source: Internet
Author: User

Overview


Sed is a stream editor. Mainly used for automatic editing of one or more files , simplifying the repeated operation of files , writing a translator , etc. This article mainly describes:

    • how SED works

    • SED command format and common options

    • Application examples

    • Advanced editing commands



how SED works


The SED command maintains two buffers during operation, one is the active "pattern space" andthe other is the auxiliary "staging buffer (holding space)". Each time the SED processes a single line of content, the entire file is processed line by row and then output line by row. When processing, the currently processed row is stored in the pattern space, followed by the SED command to process the contents of the buffer, after processing, the contents of the buffer is sent to the screen. Then the next line is processed, so it repeats until the end of the file. The working process is as follows:

650) this.width=650; "style=" float:left; "src=" http://s5.51cto.com/wyfs02/M02/85/E2/ Wkiol1etavub0byaaatnl2cw-ws236.gif "title=" 1470804204883479.gif "alt=" Wkiol1etavub0byaaatnl2cw-ws236.gif "height= "349" width= "751"/>

In general, the staging buffer is not available, but there are special commands to exchange data between the pattern space and the staging buffer, because all the operations of the sed on the text are in the pattern space, so by default, SED does not modify the contents of the original file, All output lines after processing are printed to the screen unless you use redirection to store the output.


sed command format and common options usage:
sed [OPTION] ... PATTERN [Input-file] ...
OPTION:
-N: Do not output the contents of the mode space to the screen;-e script,--expression=script: multi-point editing;-f/path/to/sed_script_file an edit command per line;-R,-- Regexp-extended: Supports the use of extended regular expressions;-i[suffix],--in-place[=suffix]: Direct editing of the original file, #一般不建议-I, for file modification, it is strongly recommended to back up first, that is-I. CHAR. (. Char is a backup file suffix name that can be customized.) )
PATTERN:

PATTERN is a specific set of SED commands (often called SCRIPT) that must be included in a pair of single quotes in order to avoid being interpreted by the shell in the following format:

' [address-range]sed-command'

Address-range refers to the scope of the line to be processed, also called the address definition;

Sed-command is an sed command that is used to process a specified row.

Address definition:
1. Empty address: The full text of the processing; 2. Single address: N: Specifies the nth row;/pattern/: Each row that is matched by this pattern; 3. Address range N,m: line n starts to line M N,+m: line n begins, to the following M-line n,/pat1/: line n begins, to first match PAT1 row/pat1/,/pat2/: first match pat1 line starts, to first match PAT2 row $ : Last line; 4. ~ Step: n~m: Starting at line N, each step M-line: All odd rows 2~2: All even lines
Edit command:
D: delete; p: Display the contents of the mode space; a \text: Append the text "text" After the line, support to use \ n to implement multiline append, I \text: Insert text "text" in front of the line, support using \ n to implement multiline insertion; C \ Text: Replace the matched line with the text "text" specified here; W/path/to/somefile: Save the line of the pattern space to the specified file; r/path/from/ Somefile: Reads the contents of the specified file to the line that the current file is matched to; file merge; =: Prints line number for the line to which the pattern matches;!: conditional inversion, address delimitation! Edit command; s///: Find and replace, its delimiter can be specified by itself, commonly used [email protected   ]@@, s## #等; Replace tag: g: Global substitution; W/path/to/somefile: Saves the result of the substitution success to the specified file; p: Displays the row where the substitution was successful; {COMMANDS}: You can enclose multiple commands to handle a single address or range of addresses. Sed-n ' 5,9p;= ' sed-n ' 5,9{p;=} '
Advanced editing Commands:
H: The content of the pattern space is covered in the holding space; h: Append the contents of the pattern space to the holding space; G: the contents of the holding space are covered in the pattern space; G: Append the contents of the holding space to the pattern space; x: Swaps the content in the pattern space with the content in the hold space N: Overwrites the next line of the row to the pattern space where the match is fetched, N: Appends the next line of the row to the pattern space, D: Deletes the row in the pattern space, and D: Deletes all rows in the multiline mode space;

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/85/E2/wKiom1eta1exk3UoAAKrutPiBuU199.png "title=" 1470833317496604.png "alt=" Wkiom1eta1exk3uoaakrutpibuu199.png "height=" 555 "width=" 801 "/>

Application Examples

There may be minor changes to the command implementations of different system environments or SED versions, but the usage is basically the same, and the following demos are based on the environment below:

    • OS: CentOS Linux release 7.2.1511 (Core)

    • sed version : sed (GNU sed) 4.2.2

Demo File 1:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/85/E2/wKioL1eta82y3vh2AACAhtZK--k327.png "title=" 1.png " alt= "Wkiol1eta82y3vh2aacahtzk--k327.png" height= "239" width= "$"/>

[Email protected] ~]# >>sed ' 2p '/etc/passwd #打印第二行

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/85/E2/wKiom1eta-XRirHwAAAj65S-yq0600.png "title=" 2.png " alt= "Wkiom1eta-xrirhwaaaj65s-yq0600.png" height= "141" width= "504"/>

[Email protected] ~]# >>sed-n ' 2p '/etc/passwd #仅打印第二行

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/85/E2/wKioL1etbADwpUhTAAAN7BetPl4501.png "title=" 3.png " alt= "Wkiol1etbadwpuhtaaan7betpl4501.png" height= "width="/>

[[email protected] ~]# >>sed-n ' 1,4p '/etc/passwd #仅打印第1到第4行 (range address)

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/85/E2/wKiom1etbFuzOydvAAAXCCLVVMY307.png "title=" 1.png " alt= "Wkiom1etbfuzoydvaaaxcclvvmy307.png" height= "width=" 501 "/>"

[[email protected] ~]# >>sed-n '/root/p '/etc/passwd #仅打印匹配root的行 (single address pattern matching)

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/E2/wKiom1etbHGjsNTzAAAUt9_pOcQ121.png "title=" 2.png " alt= "Wkiom1etbhgjsntzaaaut9_pocq121.png" height= "width=" 501 "/>"

[[email protected] ~]# >>sed-n ' 2,/root/p '/etc/passwd #仅打印从第2行开始 to the line that matches the root (range address)

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/85/E2/wKioL1etbLajQZyEAAArf6VNpL0214.png "title=" 1470916193897319.png "alt=" Wkiol1etblajqzyeaaarf6vnpl0214.png "height=" 176 "width=" 501 "/>

[[email protected] ~]# >>sed '/root/a\superman '/etc/passwd #sed-command Demo: A: Append text after matching line

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/E2/wKioL1etbM_j-0dsAAA3YqnafRQ558.png "title=" 1470916991349380.png "alt=" Wkiol1etbm_j-0dsaaa3yqnafrq558.png "height=" 301 "width="/>

[[email protected] ~]# >>sed '/root/i\superman '/etc/passwd #sed-command Demo: I: inserting text before matching lines

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/85/E2/wKiom1etbOrQ654eAABCv9xAKi0136.png "title=" 1470917171936554.png "alt=" wkiom1etborq654eaabcv9xaki0136.png "height=" 212 "width="/>

[[email protected] ~]# >>sed '/root/c\superman '/etc/passwd #sed-command Demo: C: Match line replaced with specified text

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/E2/wKioL1etbRmgFAiIAAAyMVuxla8805.png "title=" 1470917311227116.png "alt=" Wkiol1etbrmgfaiiaaaymvuxla8805.png "height=" 219 "width=" 499 "/>

[[email protected] ~]# >>sed-n ' s/root/&superman/p '/etc/passwd #sed-command Demo: s: Find root, replace it with &superman (& amp; represents a reference to a matching lookup)

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/85/E2/wKioL1etbS_QIq9GAAAYvKYsbnM784.png "title=" 1470917900411086.png "alt=" Wkiol1etbs_qiq9gaaayvkysbnm784.png "height=" "width=" 505 "/>"

[[email protected] ~]# >>sed ' s/root/superman&/g '/etc/passwd #sed-command Demo: s: Find replacement, compare the previous example, p replaces only the first matched text per line, G makes a global match

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/E2/wKiom1etbW2RGjWiAAA6oh-ga0g136.png "title=" 1470917917783416.png "alt=" Wkiom1etbw2rgjwiaaa6oh-ga0g136.png "height=" 199 "width=" 502 "/>

Demo File 2:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/E2/wKioL1etbYmgGjpXAAAaKDdmRfk412.png "title=" 1470918315995520.png "alt=" wkiol1etbymggjpxaaaakddmrfk412.png "height=" 321 "width=" 502 "/>

[[email protected] ~]# >>sed-n '/^$/= ' testfile #sed-command Demo: = Display line number (^$ blank line)

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/85/E2/wKiom1etbaDzVokyAAAO0H-70Ok381.png "title=" 1470918449906653.png "alt=" Wkiom1etbadzvokyaaao0h-70ok381.png "height=" width= "502"/>

[[email protected] ~]# >>sed-n-E '/^$/p '-e '/^$/= ' testfile #选项-e reference

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/85/E2/wKiom1etbeCi5Q_NAAAQrg6NzSY713.png "title=" 1470918925101047.png "alt=" Wkiom1etbeci5q_naaaqrg6nzsy713.png "height=" width= "498"/>

[[email protected] ~]# >>sed '/^$/d ' testfile #sed-command Demo: D Delete matching rows

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/E2/wKiom1etbi6Rf1vfAAAclANiF1U204.png "title=" 1470919019303625.png "alt=" Wkiom1etbi6rf1vfaaaclanif1u204.png "height=" 184 "width=" 488 "/>

[[email protected] ~]# >>sed ' 1,10d ' testfile #范围地址第1至第10行, sed-command:d Delete Row

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/85/E2/wKioL1etblHgC81EAAAQBwVyJyU165.png "title=" 1470919100403990.png "alt=" Wkiol1etblhgc81eaaaqbwvyjyu165.png "height=" "Width="/>

[[email protected] ~]# >>sed-i.bak ' s/^test.*$/deleted/g ' testfile #选项-i application, if you need to file modification, it is strongly recommended to back up first, that is,-I. CHAR. (. Char is a backup file suffix name that can be customized.) )

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/85/E2/wKiom1etbn3jhguEAAA0D6gM8h0194.png "style=" float: none; "title=" 1470919979292691.png "alt=" Wkiom1etbn3jhgueaaa0d6gm8h0194.png "height=" width= "498"/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/85/E2/wKiom1etbn2SVBxrAAAnwnvi-BE256.png "style=" float: none; "title=" 1470919996990277.png "alt=" Wkiom1etbn2svbxraaanwnvi-be256.png "height=" "width=" "499"/>


Advanced Editing Commands
[Email protected] ~]# >>sed-n ' n;p ' Testfile.bak #打印偶数行

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/E2/wKioL1etbuTTzpbJAAApliypvh8785.png "title=" 1470964798173800.png "alt=" Wkiol1etbuttzpbjaaapliypvh8785.png "height=" 279 "width="/>

[Email protected] ~]# >>sed ' 1! G;h;$!d ' Testfile.bak #倒序显示行, equivalent to TAC command

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/85/E2/wKiom1etbvSBxiBvAAAcudVmVis492.png "title=" 1470964912771805.png "alt=" Wkiom1etbvsbxibvaaacudvmvis492.png "height=" 191 "width=" 502 "/>

[Email protected] ~]# >>sed ' $! N;$!d ' Testfile.bak

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/E2/wKioL1etbwuTQyTqAAAPhSjaNrg832.png "title=" 1470965058998864.png "alt=" Wkiol1etbwutqytqaaaphsjanrg832.png "height=" "width=" 503 "/>"

[Email protected] ~]# >>sed ' $! N ' Testfile.bak

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/85/E2/wKiom1etbybjPZA5AAAasWUx3pU051.png "title=" 1470965360218956.png "alt=" Wkiom1etbybjpza5aaaaswux3pu051.png "height=" 183 "width="/>

[Email protected] ~]# >>sed ' $!d ' Testfile.bak

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/E2/wKioL1etbz2TKagLAAANohc5O88129.png "title=" 1470965390856912.png "alt=" Wkiol1etbz2tkaglaaanohc5o88129.png "height=" width= "506"/>

[Email protected] ~]# >>sed ' G ' Testfile.bak

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/E2/wKiom1etb1Px3vOHAAAhri5RaXw737.png "title=" 1470965441436463.png "alt=" Wkiom1etb1px3vohaaahri5raxw737.png "height=" 343 "width="/>

[Email protected] ~]# >>sed ' n;d ' Testfile.bak

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/E2/wKioL1etb2aj_A07AAAS8xca6Lc365.png "title=" 1470965516464543.png "alt=" Wkiol1etb2aj_a07aaas8xca6lc365.png "height=" 108 "width=" 499 "/>


Conclusion

Personally, the SED looks for more command operations after matching rows compared to grep, providing a more powerful D-delete and S-replace function. Mastering a skill is multi-use, in understanding the above demonstration premise, in the actual application of the longitudinal deep digging, and people to share the discussion.

Because of the beginner Linux, limited to my level, the text is inevitably wrong. Welcome you crossing feel free, discuss related technology. Thank you.

Contact me: [email protected]

This article is from the "Zhaoqi Learn to Linux" blog, so be sure to keep this source http://zhaoqifly.blog.51cto.com/3500118/1837304

Linux Basics detailed sed command

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.