sed for text processing tools on Linux systems

Source: Internet
Author: User
Tags processing text line editor

SED command


Sed is an online editor, line editor, processing a line at a time, while processing, the current processing of the row is stored in a temporary buffer, the buffer is called "pattern space", and then the SED command to process the contents of the buffer, after processing is complete, The contents of the buffer are sent to the standard output, and then the next line is processed, the same operation is repeated until the end of the file, and the contents of the object file are not changed during the process of the SED, unless a redirect is used to store the processed results. SED is mainly used to automatically edit one or more files, simplifying the process of repetitive operation of files.


SED is different from the text Processing tool we talked about Vim,grep, Vim is an interactive text editing tool, and SED is a non-interactive text editor that automatically processes files on a per-line basis given the criteria; grep is also a popular text processing tool, But grep is used to retrieve conditional keywords, while SED is the search match processing Text tool.


SED has the functions of: data substitution, deletion, addition, and so on, the data here can be some keywords, can also be some specific rows. The function of SED is quite rich.


Sed

sed [OPTION] ... ' Script ' [Input-file] ...

Script

Address Delimitation Edit Command


Common options:

-N: does not output the contents of the mode space to the screen;

-e script,--expression=script: multi-point editing;

-f/path/to/sed_script_file

One edit command per line;

-R,--Regexp-extenden: supports the use of extended regular expressions;

-i[auffix],--In-place[=suffix]: Directly edit the original file;


Address delimitation:

1) Empty address: processing the full text

2) Single Address:

#: Specify line;

/pattern/: Each line that is matched to by the word pattern;

3) address range;

#,#;

#,+#;

#,/pat1/

/pat1/,/pat2/

4) step in; ~

1-2: All Odd lines

2-2: All even lines


Edit command:

D: Delete

P: Display the contents of the mode space

A\text: Append text "text" After line, support multi-row append using \ n

I\text: Inserting text "text" After a line, enabling multiple-row insertion using \ n

C\text: Replace the matched line with the text "text" specified here


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: To cover the contents of the holding space in the pattern space;

G: Append the contents of the holding space to the pattern space;

x: Swap 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 in the read match;

N: Append the next line of the row to the pattern space to read;

D: Delete rows in the pattern space;

D: Delete all rows in multi-line mode space;


Example:

Sed-n ' N;p ' FILE: Show even rows

Sed ' 1! G;h;$!d ' file: Displays the contents of the files in reverse order

Sed ' $!d ' FILE: Take out the last line

Sed ' $! n;$! D ' file: Two lines after removing the file

Sed '/^$/d; G ' FILE: Delete all of the original blank lines, and then add a blank line after all the non-blank lines;

Sed ' n;d ' FILE: Show Odd lines

Sed ' G ' FILE: Adds a blank line behind the original line;


This article is from the "11798474" blog, please be sure to keep this source http://11808474.blog.51cto.com/11798474/1835779

sed for text processing tools on Linux systems

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.