SED command Application detailed

Source: Internet
Author: User

SED application Detailed

SED is a text processing tool, one of the Three Musketeers, which is a stream editor that processes only one line at a time. 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. SED does not modify the original file content unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, simplify the repeated operation of the file, write the conversion program and so on.

Usage: sed [OPTION] ... {Script-only-if-no-other-script} [Input-file] ...

Sed can process text files according to script, and Skript also contains address delimitation and editing commands. Address delimitation is to specify the scope of the file to be processed, such as processing only the first 10 lines of the file, or only the odd line of the file, etc., the Edit command refers to the content of the specified range of processing, such as printing on the screen display, or delete, replace, etc., the address delimitation and editing commands must be used together, If you do not specify an address delimitation, the full text is processed by default. The SED itself also has option parameters, and the common options parameters are as follows:

-N: Only the results of script processing are displayed, and sed automatically prints the contents of the mode space by default

-E: Multiple script can be specified to process simultaneously

-f/path/to/script_file: Reads the script from the specified file and runs

-R: Supports the use of extended regular expressions

-I: Modify source files directly

The way the script addresses are bound is:

(1) If the scope is not specified, the full text is processed.

(2) #: #代表数字, specify line number

(3)/pattern/: pattern matching, all rows matched by/pattern/

(4) #1, #2: Indicates all rows from page # # to Line # #

(5) #,+n: represents all rows starting from line # and then down n rows

(6)/pat1/,/pat2/: Represents the first line that is matched to by the/pat1/, to the end of the line that is first matched to the/pat2/, and all the rows in the middle

(7) #,/pat1/: Indicates the line that starts at line # and ends with the first line that is matched to the/pat1/.

(8) First~step: Specifies the starting line and the step size, such as 1~2,1 for the starting line, 2 for the step, the matching line is 1,3,5,7 ... (odd line)

(9) $: Indicates the last line

Script Edit commands are:

D: Delete rows in the pattern space

[email protected] testdir]# Cat test # Check the contents of the test original file first 1aaaaaaaaaaaaaaaa2bbbbbbbbbbbbbbbb3cccccccccccccccc4dddddddddddddddd5eeeeeeeeeeeeeeee6ffffffffffffffff [[Email protected] testdir]# sed ' 1d ' test # Delete line 1th 2bbbbbbbbbbbbbbbb3cccccccccccccccc4dddddddddddddddd5eeeeeeeeeeeeeeee6ffffffffffffffff

P: Lines in print mode space

[[email protected] testdir]# sed-n '/aaa/p ' Test #只打印匹配 "AAA" line 1aaaaaaaaaaaaaaaa

A \text: Append text below the current line, support for multi-line append using \ n

[[Email protected] testdir]# sed ' 1,3a\ggggggggg ' test #在1至3行下面插入 "GGGGGGGGG" 1aaaaaaaaaaaaaaaaggggggggg2bbbbbbbbbbbbbbbbggggggggg3ccccccccccccccccggggggggg4dddddddddddddddd5eeeeeeeeeeeeeeee6ffffffff Ffffffff

I \text: inserting text below the current line, enabling multiple-line insertion using \ n

[Email protected] testdir]# sed ' 2,+2i\ggggggggg ' test #在从第2行开始向下2行的上面插入 "GGGGGGGGG" 1aaaaaaaaaaaaaaaaggggggggg2bbbbb Bbbbbbbbbbbggggggggg3ccccccccccccccccggggggggg4dddddddddddddddd5eeeeeeeeeeeeeeee6ffffffffffffffff

C \text: Change the selected line to a new text

[[Email protected] testdir]# sed '/a/,/c/c\ggggggggg ' Test #将第一次匹配到的 ' a ' line to ' C ' where all the content is changed to ' GGGGGGGGG ' Ggggggggg4dddddddddddddddd5eeeeeeeeeeeeeeee6ffffffffffffffff

=: Prints line numbers for lines in pattern space

[[email protected] testdir]# sed-n ' 3,/f/= ' test #只显示从第3行开始到第一次匹配到 line number of all lines between lines of "F" 3456

s/regexp/replacement/: Find replacement, replace the content that is matched by regexp to replacement, support the use of a different delimiter, provided that the delimiter does not appear in the following pattern, such as [email protected]@@ or s### , there are post options for replacements, as follows:

G: Global Substitution

P: Show the rows that were successfully replaced

I: Case insensitive

[[Email protected] testdir]# sed ' 1~2s/e/g/' test #将奇数行中匹配到的第一个 "E" replaced with "G" 1aaaaaaaaaaaaaaaa2bbbbbbbbbbbbbbbb3cccccccccccccccc4dddddddddddddddd5geeeeeeeeeeeeeee6ffffffffffffffff[[email Protected] testdir]# sed ' 1~2s/e/g/g ' test #将奇数行中所有匹配到的 "E" replaced with "G" 1aaaaaaaaaaaaaaaa2bbbbbbbbbbbbbbbb3cccccccccccccccc4dddddddddddddddd5gggggggggggggggg6ffffffffffffffff[[email Protected] testdir]# sed-n ' 1~2s/e/g/p ' Test #将奇数行中所有匹配到的 "E" replaced with "G" and display only the substituted row 5geeeeeeeeeeeeeee

W/path/to/somefile: Save the specified content to the file specified by the/path/to/somefile path

[[email protected] testdir]# sed-n ' 1,4w/testdir/testsave ' test #将test文件中的1至4行内容另存到/testdir/file under the directory [[email PR Otected] testdir]# cat Testsave #查看testsave文件1aaaaaaaaaaaaaaaa2bbbbbbbbbbbbbbbb3cccccccccccccccc4dddddddddddddddd

R/path/form/somefile: Inserts all the contents of another file at the specified location in the file, completing the file merge

[Email protected] testdir]# sed ' 3r/testdir/text ' test #将/testdir/ The contents of the text file are inserted below line 3rd of the test file 1aaaaaaaaaaaaaaaa2bbbbbbbbbbbbbbbb3cccccccccccccccchhhhhhhhhhhhhiiiiiiiiiiiiijjjjjjjjjjjjj4ddddd Ddddddddddd5eeeeeeeeeeeeeeee6ffffffffffffffff


In addition to the pattern space, the SED has a hold space. SED works by reading a line of text into the pattern space, processing in the pattern space, processing the result output value standard output device, processing a line of content in the pattern space regret to continue processing the next row, then the processed rows may have other processing, so the processed rows can be "delivered" To the hold space, and then "transfer" back to the mode space in subsequent processing.

The SED tool supports a number of advanced commands to be used in the hold space, these advanced commands are:

H: Overwrite the contents of the hold space with the content in the pattern space

H: Append the contents of the pattern space to the hold space

G: Remove data from hold space to pattern space

G: Append data from Hold space to mode space

x: Swap to preserve space and pattern space content

N: Reads the next line of the matched row to overwrite the pattern space

N: Reads the next line of the matched line to the pattern space

D: Delete the contents of the pattern space

D: Delete the first row in the multiline mode space

Attention:

!: Inverse, indicating that the following command is acting on all rows that are not selected

;: Semicolon can be used for splitting scripts


Example 1:

[Email protected] testdir]# sed-n ' n;p ' test2bbbbbbbbbbbbbbbb4dddddddddddddddd6ffffffffffffffff

The processing steps for SED are:

(1) Because n means that the next line of the matched row is read into the pattern space, the action of the first reading of line 1th is to overwrite the contents of the 2nd row into the pattern space, where the contents of the pattern space are the contents of the 2nd line

(2) Execute P operation immediately, that is, print the line of the mode space (print the 2nd line of content)

(3) Then repeat the above operation, continue to read the next line (that is, line 3rd, because the 2nd row has been read), and the next line is overwritten to the pattern space (that is, the contents of the schema space is the 4th line), and then perform the P operation (print the 4th line), and so on, until the completion of the file


Example 2:

[[Email protected] testdir]# sed ' 1! G;h;$!d ' Test6ffffffffffffffff5eeeeeeeeeeeeeeee4dddddddddddddddd3cccccccccccccccc2bbbbbbbbbbbbbbbb1aaaaaaaaaaaaaaaa

The processing steps for SED are:

(1) Read the first line of the file into the pattern space, because the first line does not perform G operations

(2) Perform H operation to cover the contents of the pattern space to maintain space

(3) because it is not the last line, execute the D operation and delete the contents of the pattern space. The mode space content is empty, keeping the space as the first line of content

(4) immediately executes the 2nd loop, reads the 2nd line, and performs a G operation to append the contents of the hold space to the pattern space. The pattern space is the 2nd and 1th lines, keeping the space at the 1th line

(5) Continue the H operation, and then overwrite the contents of the pattern space to hold space, because line 2nd is not the last line, and then executes the D operation to delete the contents of the pattern space. The mode space is empty and the space is the 2nd and 1th lines (line 1th below the 2nd line)

(6) loop to the last line of the file, do not perform the D operation, at this time the 1th line of the pattern space is the last line of the source file (row order), and finally print out the contents of the mode space.

This article is from the "Linux Operational Learning path" blog, please be sure to keep this source http://fengliang.blog.51cto.com/3453935/1836676

SED command application detailed

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.