Shell's sed command

Source: Internet
Author: User

SED is designed to handle all editing tasks at once, particularly efficiently, saving users a lot of time, and SED is suitable for the following three scenarios:

1. Edit files that are too large relative to the interactive text editor;

2, editing commands too complex, in the interactive text editor difficult to enter the situation;

3, scan the file once, but need to perform a number of editing functions of the situation;

Sed simply edits a copy of the original file in the buffer and does not edit the original file. Therefore, if you need to save the changes and you need to redirect the output to another file, you can use a command in the following format:

sed ' sed command ' input_file > Result_file

This command saves the SED command changes to the Input-file to Result-file, and the ">" symbol is the redirect symbol;

Three ways to invoke SED:

1. Call sed directly in the shell in the format: sed [options] ' sed command ' input file//must enclose the SED command in single quotation marks

2. After inserting the SED command into the script file, it is then invoked through the SED command in the following format:

sed [options]-F sed script input file

3. Execute the SED script directly, in the following format:

./sed script File Input file

The SED command identifies how the text is handled. such as printing, deleting, appending, inserting, replacing, etc.

How the SED command locates text:

Sed-n ' 1p ' input//prints only the first line of input, and without the-n option, not only the 1th line, but also the entire contents of input will be printed;

Sed-n ' 3, 6p ' input//print 第3-6 line only

Pattern matching using the/pattern/method

Sed-n '/certificate/p ' input//print matching Certificate line, case sensitive

Sed-n/certificate/, 6p ' input//print lines matching Certificate lines to 6

Sed edit command The a\ symbol is used to append text, which appends one or more lines of the specified text to the specified line. If you do not specify a text append position, the SED is placed by default after each line, and the appended text is in the following format:

Sed ' Specify address a\text ' input file

Note: The specified address is given in the form of a matching pattern/pattern/or line number, which is used to position the appended position of the new text, and the SED appends the \a text (adding a new line);

    Special Note: When SED completes the append text function, it only outputs the result to standard output, and the original file does not make any changes;

1 /bin/sed          -F #必须加上-f option, without this option, the script executes an error 2 /test/a\                #a \ Represents adding text after the row where test is located 34#添加内容 5 We append a new line. \    #"\" symbol means newline 6 We append  another line.

  The "$" symbol, which represents the end of the line in the regular expression, particularly note that in sed, "$" represents the last line;

Sed-n ' 2, 10!p ' input//indicates a line that prints out non-2--10 rows;

Insert text: sed ' Specify address i\text ' input file//with ' a\ ' just the opposite, the row is split into text before the row in the specified position (text is a new row)

  

    

Shell's sed command

Related Article

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.