Shell script learning-8 (SED command learning)

Source: Internet
Author: User

Example 10What is the execution sequence of many-e commands?

[Rocrocket @ rocrocket programming] $ cat mysed.txt
Beijing 2003
Beijing 2004
Beijing 2005
Beijing 2006 Beijing 2007
Beijing 2008
[Rocrocket @ rocrocket programming] $ sed-E's/Beijing/London/G'-e '/Beijing/d' mysed.txt
London 1, 2003
London 1, 2004
London 1, 2005
London 1, 2006
London 1, 2007
London 1, 2008

The last-E contains the row in which Beijing is deleted, but all rows are output in the final result. In fact, this is caused by the execution order, the first-E has replaced Beijing with London, so no wonder the second-e cannot find Beijing.

Example 11Read the content of a specific file and insert it to the specified location of the file:

[Rocrocket @ rocrocket programming] $ cat ins.txt
==== China ====
[Rocrocket @ rocrocket programming] $ cat mysed.txt
Beijing 2003
Beijing 2004
Beijing 2005
Beijing 2006
Beijing 2007
Beijing 2008
[Rocrocket @ rocrocket programming] $ sed '/2005/R ins.txt' mysed.txt
Beijing 2003
Beijing 2004
Beijing 2005
==== China ====
Beijing 2006
Beijing 2007
Beijing 2008

This command is used to insert the content of the ins.txt file to a row containing characters.

Example 12Write certain specified rows to a specific file

[Rocrocket @ rocrocket programming] $ sed '/200 [4-6]/W new.txt 'mysed.txt
Beijing 2003
Beijing 2004
Beijing 2005
Beijing 2006
Beijing 2007
Beijing 2008
[Rocrocket @ rocrocket programming] $ cat new.txt
Beijing 2004
Beijing 2005
Beijing 2006

Check that w new.txt.pdf writes a row containing million, million, million to the new.txt file.

Example 13Use a/In a specific line"Below"Insert specific content

[Rocrocket @ rocrocket programming] $ cat new.txt
Beijing 2004
Beijing 2005
Beijing 2006
[Rocrocket @ rocrocket programming] $ sed '/2004/A/China' mysed.txt
Beijing 2003
Beijing 2004
China
Beijing 2005
Beijing 2006
Beijing 2007
Beijing 2008

Use a/and add the content to be added.

Example 14Use I/in a specific line"Above"Insert specific content

[Rocrocket @ rocrocket programming] $ sed '/2004/I/China' mysed.txt
Beijing 2003
China
Beijing 2004
Beijing 2005
Beijing 2006
Beijing 2007
Beijing 2008

Example 15Replace a specific character

[Rocrocket @ rocrocket programming] $ cat mysed.txt
Beijing 2003
Beijing 2004
Beijing 2005
Beijing 2006
Beijing 2007
Beijing 2008
[Rocrocket @ rocrocket programming] $ sed 'y/eijng/'mysed.txt
Beijing 2003
Beijing 2004
Beijing 2005
Beijing 2006
Beijing 2007
Beijing 2008

If y is used, each character in the first column is replaced with the character in the second column.

Example 16Process the next row of the matched row

[Rocrocket @ rocrocket programming] $ cat mysed.txt
Beijing 2003
Beijing 2004
Beijing 2005
Beijing 2006
Beijing 2007
Beijing 2008
[Rocrocket @ rocrocket programming] $ sed '/2004/{n; y/eijng/;}' mysed.txt
Beijing 2003
Beijing 2004
Beijing 2005
Beijing 2006
Beijing 2007
Beijing 2008

In this example, find the row containing 2004, and replace eijng in the row below with eijng in upper case. Here "N;" plays the role of "moving to the next line. The meaning behind N is actually to put the next row in the mode space.

[Rocrocket @ rocrocket programming] $ cat mysed.txt
Beijing 2003
Beijing 2004
Beijing 2005
Beijing 2006
Beijing 2007
Beijing 2008
[Rocrocket @ rocrocket programming] $ sed '/200/{n; y/eijng/;}' mysed.txt
Beijing 2003
Beijing 2004
Beijing 2005
Beijing 2006
Beijing 2007
Beijing 2008

This example is more typical. You can find that Beijing appears in the same line. Do you know why? I believe you can come up with the answer!

PS: SED's stream Art series has ended here :)

PS: Actually, sed is still very knowledgeable, and regular expressions also have a lot to learn. I am also learning... This small series should be used as an example. I hope it will be useful to you!

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.