SED command Use (note)

Source: Internet
Author: User

Three major Linux text processing tools grep sed awk

Before there was said grep today look at the SED command

Brief introduction

The SED stream Editor, a non-interactive editor that processes a single line of content at a time. And the editor does not modify the original file by default if you want to modify the result using shell redirection. By default, all output lines will be printed on the screen.

The SED editor processes the file (or input) row by line and sends the results to the screen. The process is as follows:

First, the SED saves the row currently being processed in a temporary buffer (also known as pattern space), then processes the rows in the temporary buffer and sends the line to the screen when it is complete. The SED deletes its buffers every time a row is processed, and then reads the next line in, processing, and displaying. After processing the last line of the input file, sed runs to the end. The SED has a temporary buffer in each row and edits the copy, so the original file is not modified.


SED basic format

sed [option] "Addresscommand" file


Address

The righteousness is to specify which line in the file or the row to be positioned to support the regular expression, number. If no row is specified, all rows are processed by default.

    1. Startlike,endline Example

1,100 locate the first line to 100 lines in the text

$ represents the last line

$-1 represents the penultimate line

2./regexp/middle is the normal expression example

/^root/matches a row that starts with root

3./pattern/,/pattern2/matches all rows in the middle of the line that match from the first time it was pattern1 to, to the end of the row that was first matched to the PATTERN2

4. linenumber give only one number, then exactly match to that line

5. Startline,+n N is a number indicating that n rows are matched backwards from the startline line.


Command

This is the match to the front of the line to do word processing, such as delete, modify, replace and so on

D Delete rows that match the criteria

A \ "string" Can be unquoted, appending a new line after the qualifying row, with the contents of string. \ n is a newline character.

i \ "string" Can be unquoted, appending a new line to the front of the qualifying line, with the contents of string. String means what you output from this place, and what is added behind the line.

P Displays rows that match the criteria, which by default causes the number of rows to be displayed two times (this is because of patter space)


[option]

This option is optional and can be used or not

-n silent mode, which does not display the contents of the mode space by default.

SED command Use (note)

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.