SED command basic syntax (i)

Source: Internet
Author: User

SED command basic syntax (i)

Learning Goals

Sed workflow

SED command basic syntax

1. Sed workflow

The SED workflow is divided into the following steps

Flowchart Explanation: First read a line stored in the pattern space (pattern spaces);

Secondly, the content is obtained from the pattern space, and the command is parsed.

Then the contents of the pattern space are emptied and output;

Finally, repeated reading;

2. Basic syntax of sed command

(1) format of the SED command

Sed[options]{sed-commands}{input-file}

(2) sed p command with-n option (print,--quiet)

Sed ' P ' test.txt this will output two times per line, (read-in mode space output once, empty mode space and then output once)

Sed–n ' P ' test.txt plus-n is the same as the cat command, only the contents of the mode space are output;

Sed–n ' 1 P ' test.txt only prints the first line;

Sed–n ' 1,3 p ' test.txt printing line;

Sed–n '/hello/p ' test.txt print Hello line;

Sed–n '/hello/,/hello1/p ' test.txt print hello to Hello1 line;

Sed–n '/^hello/p ' test.txt prints the line at the beginning of the Hello; (regular expression)

Sed–n '/hello$/p ' test.txt prints the line at the end of the Hello; (regular expression)

Sed–n '/^[0-9] \{3\}/p ' test.txt print the number repeat 3 times the content;

Sed–n ' Test.txt P ' is printed once every other line (2-1=1);

(3) The D command (delete) of the SED, similar to the P command;

Sed '/103/d ' test.txt delete the line where mode space 103 is located, and the contents of the text file (test.txt) do not change;

(4) More SED options

-F Read command from File

Sed–f cmd.sed test.txt New cmd.sed command file, the file contents are as follows:/hello/p

Multiple commands can be written in the command file;

-e option to execute multiple commands

Sed–n-e '/hello/p ' –e '/hello1/p ' test.txt prints the line where Hello and Hello1 are located;

SED command basic syntax (i)

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.