linux-sed command

Source: Internet
Author: User

About SED

Sed is a stream editor that reads one line of text at a time, puts it into the memory buffer (pattern space), and then uses the SED command to process the contents of the pattern space, which is output to the screen (stdout) By default after processing is complete. The next row of data is then processed.

SED working characteristics

    • Do not edit the source file directly, copy one copy to pattern space, and process the contents of the pattern space in memory.
    • The processing result is output to stdout by default.

 Note: In fact, SED has a holdspace, but its basic process

How to use SED

  sed [OPTION] ' script ' [input-file

    OPTION:

-n:slient silent mode, typically, data from standard input (stdin), the default output to the screen (stdout). But with-N, only the content that has been specially processed by SED will be listed. 、

-E Script: Multi-point editing, multiple editing commands for a row of data.

-f/path/to/sed_script_file: Processing data using a script. Each action in the script is an edit command.

-R: Supports extended regular expressions.

-i[suffix]: Edit the original file directly.

    The content in the Script:script is the ' address bound Edit command ' (directly followed by the edit command after the address is bound).

      Address delimitation: Empty address, single address, range address, address stepping

Empty address: Do not give address defaults to full-text processing.

Single Address:

#: Specifies a row.

/pattern/: Pattern matching row.

Scope Address:

NS,ND: from NS line to ND line.

Ns,/pattern/: The line to which the pattern matches from the NS line.

ns,+#: Offset # line starting from NS line.

/pattern1/,/pattern2/: Rows that match from PAT1 to PAT2.

Address Stepping: ~

One to one: starting from the first line, step 2, that is, all odd lines.

      Edit command:

D: Delete content in pattern space--delete output mode space content to default output

          

Using the-n option will not output anything.

          

       P: Print the contents of the mode space. Normally p runs with the parameter sed-n, outputting only what SED has processed

Printed two times 3, 4 rows of content, the first output of the "#/etc/fstab" is the default output, the second output of "#/etc/fstab", is sed to read the contents of the file to the third line is the P command

(print) processing. So there will be two lines.

          

Using-N, the default output is masked to the output of content that has been processed by SED.

          

         

       A \text: Append, append a row after the matching row with new rows of data \text. You can append multiple rows using \ n implementations.

The first behavior is blank, and the new row is in the second line.

          

Use \ n to implement multiple-row insertions.

          

       I \text: Insert a row that inserts a new row of data into the \text row before the matching line. You can append multiple rows using \ n implementations.

The first action is a blank line, and the new row is inserted before the empty line.

          

Use \ n to implement multiple-row insertions.

            

       C\text: Replaces the matched line with the content of the \test.

Replace the line that begins with # as NULL.

          

          

Use the-e parameter to process the same data multiple times.

          

        W/path/to/somefile: Save the contents of the mode space to the specified file

First delete the lines that begin with # and then output the remainder to a file

               

R/path/from/somefile: Reads the specified file contents to the line that the current file is matched to to implement the file merge.

Add the contents of the/etc/issue file to the first line of/etc/fstab.

          

        =: The line number is inserted on the previous line of the matched line

Insert line number in 第1-3 line.

          

        ! : Conditional inversion,! To write in front of the edit command behind the pattern--address delimitation! Edit the command.

Delete all lines that do not begin with #.

          

        s///: Find replacement, delimiter can be self-specified, commonly used are [email protected]@@,s## #等.

Replace tag:

G: Global substitution.

W: Saves the result of the replacement success to the specified file.

P: Displays the row where the replacement was successful.

Exercise one: Remove whitespace characters that begin with a space in the/boot/grub/grub.conf file

Sed ' [email protected]^[[:space]]\[email protected]@ '/boot/grub/grub.conf

Exercise two: Remove all whitespace characters from the # and # numbers in the/etc/fstab file that begin with # at the beginning of #

Sed ' [email protected]^#[[:space]]*@@ '/etc/fstab

Exercise three: Output an absolute path to SED and take out its base directory.

echo "/var/log/messages" |sed ' [email protected][^/]\[email protected]@ '

echo "/var/log/messages/|sed" [email protected] [^/]\+/[email protected]@ '

linux-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.