A brief introduction to the SED command for Beginners Learning Linux

Source: Internet
Author: User
Tags uuid

1. How SED works

Sed (stream editor) is a stream editor, which is also a pipeline command that analyzes and edits standard input, including replacing, deleting, adding, selecting specific lines, and so on. The runtime, in the unit of behavior, processes only one row of content at a time, so it is also called the row editor. Sed can also be used in conjunction with regular expressions to simplify the repetitive operation of files.

Sed throughout the execution of the instruction, the content of the edited file itself has not been changed (unless the-i option is added), because SED before executing the instruction, the file to be processed in the first temporary transfer to its cache, this cache is called "pattern space", and then execute the relevant instructions, and outputs the processed results to a standard output device (such as a screen), then proceeds to the next line, so repeatedly until the last line. But sometimes the input of the command is more complex, the line is only processed once and can not achieve the results of the instructions, need to do other processing, then how to do? In this case, you need to apply to the hold space, the so-called space is the temporary storage of the line has not been processed, in the subsequent processing to keep the space content will be re-transferred into the mode space processing, until the instruction is complete. For example, the pattern space equivalent to the processing workshop, and maintain space equivalent to the warehouse, in the process due to the lack of materials in the process, the need to temporarily store unfinished semi-finished products, and this warehouse is used to save the processing workshop unfinished semi-finished products.

2. Grammar
sed [OPTION] ... ' Script ' [Input-file] ... Note: script consists of address delimitation and edit commands
3. Common options
-N: Silent mode, that is, the standard output of the mode space is not displayed, the default is the display of-e: Multi-point editing, that is, specify multiple script runs such as: Delete lines in the/etc/fstab file starting with # and UUID

650) this.width=650; "src=" Http://i.imgur.com/GIlkLax.png "style=" margin-top:0px;margin-right:0px;margin-bottom:0 px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px; border-right-width:0px;border-bottom-width:0px;border-left-width:0px; "alt=" Gilklax.png "/>

-F: Read script from specified file and edit-R: Supports extended regular expression, default is support basic regular expression-I: Modify source file directly (note Backup of original file)
4. Address delimitation (1) Do not give address (open site):
Processing of full-text content
(2) Single address:
N: Specifies the number of lines $: Indicates the last line/pattern/: All rows that are matched by the pattern, such as: Sed-n '/root/p '/etc/passwd
(3) Scope address:
N,+m: The contents of the nth line to the plus m line, such as: 3,+5 represents the contents of rows 3rd through 8th n,m: The contents of rows from line N to line M n,/pattern1/: represents the content between rows from nth to first being matched to a pattern 1/pattern1/,/ PATTERN2/: Represents the first time that a line matched to a pattern 1 is the same as the contents of a row that was first matched to a pattern of 2, such as: Sed-n '/^root/,/^adm/p '/etc/passwd

650) this.width=650; "src=" Http://i.imgur.com/6a2sMRw.png "style=" margin-top:0px;margin-right:0px;margin-bottom:0 px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px; border-right-width:0px;border-bottom-width:0px;border-left-width:0px; "alt=" 6a2smrw.png "/>

(4) Step forward (~):
2~2: denotes all odd rows.
5. Basic editing commands
D: Delete the contents of the mode space P: Display the contents of the mode space, if the N option is not added, the matching content is displayed two times A\text: Append the text "\text" below the matched line, and support the escape character \ n to implement multi-line append such as: sed '/root/a \magedu.com '/ etc/passwd

650) this.width=650; "src=" Http://i.imgur.com/iSIbIEf.png "style=" margin-top:0px;margin-right:0px;margin-bottom:0 px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px; border-right-width:0px;border-bottom-width:0px;border-left-width:0px; "alt=" Isibief.png "/>

I\text: Append the text "\text" to the line that matches to, support the escape character \ n to implement multi-line append such as: sed ' 2i \magedu.com\nhomework '/etc/passwd

650) this.width=650; "src=" Http://i.imgur.com/EAbpsSQ.png "style=" margin-top:0px;margin-right:0px;margin-bottom:0 px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px; border-right-width:0px;border-bottom-width:0px;border-left-width:0px; "alt=" Eabpssq.png "/>

C\text: Replace the matched line with the specified text "\text" such as: sed '/^uuid/c \uuid=this is a test '/etc/fstaw/path/to/ Somefile: Save the matching line in the pattern space to the specified file such as: Sed-n '/root/w/tmp/user.txt '/etc/passwd

650) this.width=650; "src=" Http://i.imgur.com/i4Tgw0F.png "style=" margin-top:0px;margin-right:0px;margin-bottom:0 px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px; border-right-width:0px;border-bottom-width:0px;border-left-width:0px; "alt=" I4tgw0f.png "/>

R: Reads the contents of the specified file to the line below the current pattern as follows: sed '/root/r/etc/issue '/etc/passwd

650) this.width=650; "src=" Http://i.imgur.com/K793bLe.png "style=" margin-top:0px;margin-right:0px;margin-bottom:0 px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px; border-right-width:0px;border-bottom-width:0px;border-left-width:0px; "alt=" K793ble.png "/>

=: Add line number to the line to match to: sed '/^uuid/= '/etc/fstab

650) this.width=650; "src=" Http://i.imgur.com/F15StHI.png "style=" margin-top:0px;margin-right:0px;margin-bottom:0 px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px; border-right-width:0px;border-bottom-width:0px;border-left-width:0px; "alt=" F15sthi.png "/>

!: The condition is reversed, its position is "address delimitation!" Edit command. s///: Find and Replace, delimiter "///" can be specified, such as [email protected]@@,s### #替换标记选项: g: Global Replacement P: Display replace successful row W: Save the successful row to the specified file such as: Sed-n ' [email p rotected]^uuid.* @hello, good [email protected]/testdir/tihuan.txt '/etc/fstab

650) this.width=650; "src=" Http://i.imgur.com/FpjehXX.png "style=" margin-top:0px;margin-right:0px;margin-bottom:0 px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px; border-right-width:0px;border-bottom-width:0px;border-left-width:0px; "alt=" Fpjehxx.png "/>

&: Add characters before or after a match to a character such as: [[email protected] ~]# sed ' [email protected]@&[email protected] '/etc/fstab: add hi after UUID

650) this.width=650; "src=" Http://i.imgur.com/XqzPruz.png "style=" margin-top:0px;margin-right:0px;margin-bottom:0 px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px; border-right-width:0px;border-bottom-width:0px;border-left-width:0px; "alt=" Xqzpruz.png "/>

[[Email protected] ~]# sed ' [email protected] @hi &@g '/etc/fstab: Add hi before UUID

650) this.width=650; "src=" Http://i.imgur.com/vgfLwUC.png "style=" margin-top:0px;margin-right:0px;margin-bottom:0 px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px; border-right-width:0px;border-bottom-width:0px;border-left-width:0px; "alt=" Vgflwuc.png "/>

6. Advanced editing commands
H: Overwrite the contents of the pattern space to the Save space H: Append the contents of the pattern space to the hold space G: overlay The contents of the hold space into the pattern space g: Append the contents of the hold space to the pattern space N: Overwrite the next line of the matched row to the pattern space N: Append the next line of the matched row to the pattern space X: The contents of the pattern space and the hold space Content Interchange D: Delete a line of pattern space (single row) d: Delete all rows of pattern space
7. Examples (only part of the display results) (1) Displays the even lines of the/etc/passwd file

650) this.width=650; "src=" Http://i.imgur.com/Er8SrC2.png "style=" margin-top:0px;margin-right:0px;margin-bottom:0 px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px; border-right-width:0px;border-bottom-width:0px;border-left-width:0px; "alt=" Er8src2.png "/>

(2) Reverse display/etc/passwd file contents

650) this.width=650; "src=" Http://i.imgur.com/2kyOt6X.png "style=" margin-top:0px;margin-right:0px;margin-bottom:0 px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px; border-right-width:0px;border-bottom-width:0px;border-left-width:0px; "alt=" 2kyot6x.png "/>

(3) Display the last two lines of the/etc/passwd file

650) this.width=650; "src=" Http://i.imgur.com/bEg83H5.png "style=" margin-top:0px;margin-right:0px;margin-bottom:0 px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px; border-right-width:0px;border-bottom-width:0px;border-left-width:0px; "alt=" Beg83h5.png "/>

(4) Add a blank line to the file/etc/passwd each line

650) this.width=650; "src=" Http://i.imgur.com/t7UqAjL.png "style=" margin-top:0px;margin-right:0px;margin-bottom:0 px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px; border-right-width:0px;border-bottom-width:0px;border-left-width:0px; "alt=" T7uqajl.png "/>


This article is from the "Learn Linux for Beginners" blog, please make sure to keep this source http://kop309.blog.51cto.com/9034739/1836483

A brief introduction to the SED command for Beginners Learning Linux

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.