sed command under the shell

Source: Internet
Author: User

I. SED

sed (stream editor) translated as a stream editor , since it is an editor, its main function is to edit the text content of the corresponding editing, it can be specified in a predetermined mode of one or more files to automate processing, thus simplifying the repeated operation of the file;

The basic format used by SED is:

Command line:sed [options] ... ' Edit command ' [file] ...

Scripting:sed [options] ...-f [Edit script file] [file] ...

Where the format of the edit command is:/pattern/action;

Pattern is a regular expression, action is the corresponding action, that is, the line to satisfy the regular expression corresponding action, if the pattern is omitted, it means that all the lines in the file are action action;


two . Maintain space and pattern space

The processing mode of SED is row processing , when processing, SED first will get the file content of a line, and then put in a buffer to be processed, this buffer is called "pattern space", processing finished output to the standard output, The buffer is then empty and then taken down one line to the pattern space for processing until the end of the file, the entire process does not affect the original contents of the file, unless the result is redirected back to the original file;

For SED, the buffer that handles the contents of the file is called pattern space, and the SED has another buffer called hold space . The space can be considered as the backup storage warehouse of SED, that is, the content that needs to be processed, and the pattern space is the real work area, usually it is not used to keep the space, only use the pattern space to do one line of processing, but sometimes the pattern space to complete the task needs to maintain space to assist, This requires a few advanced commands:

+g: Copy the contents of the space to the pattern space, the original content of the mode space will be emptied;

+g: Append the contents of the space to the pattern space;

+h: The contents of the schema space are copied to the hold space, and the original contents of the space are erased;

+h: Append the contents of the pattern space to the hold space;

+n: reads the next line of content, copies it to the pattern space;

+n: reads the next line of content, appends it to the pattern space;

+d: Delete all the contents of the pattern space and read the next line into the pattern space;

+d: Delete Only the first row of the pattern space, not the next line;

+x: Swap mode space and preserve the content of the space;

Simple to remember,G is get, from maintaining the space get content to pattern space, H is hold, from the mode space hold content to keep space, n means next, read into the next line, d means delete, delete the contents of the pattern space, lowercase is a copy, uppercase is appended;


Three. Chestnut time

    1. Adds a blank line to the end of each line in the file;

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/83/49/wKioL1dviJWAs3CMAAA_4XGvE5U636.png "title=" sed G.png "alt=" Wkiol1dvijwas3cmaaa_4xgve5u636.png "/>

Here to explain why only a G can be used, the first G is to keep the content of the space to append to the pattern space, while the content of the space to keep the first line is a blank line , therefore, You only need to append a blank row of space to the pattern space, which is the one line read from file, every time you read a row from file, but this does not affect the contents of the file, and if you want to change it, just add the-I option . Indicates that the result of processing will affect the original document ;

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/83/4A/wKiom1dvidPyBopdAAAxJZsx8-k703.png "title=" File.png "alt=" Wkiom1dvidpybopdaaaxjzsx8-k703.png "/>


2. Simulate TAC functions with SED

Cat is the content of the output file, and the TAC and its literal word is the reverse of cat, that is, the content of the output file in reverse order;

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/83/4A/wKiom1dvkGSAY7C9AAA9QUmd9uc796.png "title=" File.png "alt=" Wkiom1dvkgsay7c9aaa9qumd9uc796.png "/>

Because the pattern space is a row of processing the contents of the file, want to reverse the output is to use to maintain space, first of all, if the first line is not to execute G instead of the first line to read the pattern space copy to hold space is ' Hello 1 ', then delete all the contents of the pattern space and read the next line, At this time, not the first line of content ' Hello 2 ', will keep the content of the space appended to the pattern space, the mode space is ' Hello 2 \ n Hello 1 ' and then copied back together, so repeatedly, until the last line is no longer deleted, processing completed;


3. Append matching lines to end of file

Appends a row of content that satisfies the match criteria to the end of the file;

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/83/4A/wKioL1dvldPQ9B8NAAA2Ei-DnJA189.png "title=" File.png "alt=" Wkiol1dvldpq9b8naaa2ei-dnja189.png "/>

Append the content that satisfies the condition to the hold space, append the contents of keeping space to the pattern space when processing to the last line, there is a blank line in front said that the space itself will have a blank line;


4. Transformation of Ranks

Converts the row arrangement in a file to a column arrangement;

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/83/4A/wKioL1dvly_Q5YHbAAAbss2wjyk824.png "title=" File.png "alt=" Wkiol1dvly_q5yhbaaabss2wjyk824.png "/>

Because if you want to convert the pattern space to have at least a row of data, so you can read into a row of data into the pattern space when it is appended to the hold space, when processing to the last row of the mode space and keep the content of the space exchange, when the pattern space inside the file content, and then the overall will ' \ n ' replaced with a space;


5.1~100 summation

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/83/4B/wKiom1dvmxKTjbV4AAAshtMXhms992.png "title=" File.png "alt=" Wkiom1dvmxktjbv4aaashtmxhms992.png "/>seq represents a sequence from 1 to a specified number, and then gives the sequence to sed, first appending each row to the hold space, If the last line is to exchange the contents of the space and mode space, then the content of the post-swap mode space is processed, all the ' \ n ' are replaced with ' + ', and then handed to the BC Computing tools to deal with it;


Another way of seeking 1~100 and:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/83/4A/wKioL1dvnTXCLBbCAAAqE-LLFDk325.png "title=" File.png "alt=" Wkiol1dvntxclbbcaaaqe-llfdk325.png "/>

: A can be seen as a label, n means to read it continuously and append it to the pattern space, the BA means to jump to the label, that is, if the last line is read and read continuously until the last line is appended, the contents of the pattern space is the contents of the sequence, and then the whole will be ' \ n ' replaced with ' + ' And, likewise, give it to BC to calculate and;


6. Print odd and even lines

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/83/4A/wKioL1dvntKBkWNMAAAzqkCT8Jc766.png "title=" File.png "alt=" Wkiol1dvntkbkwnmaaazqkct8jc766.png "/>

For n is read into the next line to append to the mode space, and n is read into the next line and copied to the pattern space, where the copy will be the original value of the pattern space is overwritten, so the first n and then P operation is printed out is even line, and the first P operation and then read into the copy print is odd line;



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1793042

sed command under the shell

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.