"Stream Art of the SED III"-linux command five-minute series 23

Source: Internet
Author: User

This original article belongs to "Linux greenhouse" blog, the blog address is http://roclinux.cn. The author of the article is rocrocket.

In order to prevent the vicious reproduction of some websites, special in each article before adding this information, but also hope that readers understand.

===

[Start of body]

Next: "The stream art of the SED II"-linux command five-minute series 22

Example five to insert something after some string

[email protected] programming]$ cat Mysed.txt
Beijing
London[[email protected] programming]$ sed ' s/b.*/&2008/' mysed.txt
Beijing2008
London

The function of this command is to add 20,084 characters after the place containing ' b.* '. This command uses the & character, this is a skill, master and rookie than, these skills are very important oh. & Says "The part that is being matched", here is ' Beijing '!

Example six will line the first Beijing after adding 2008, after the last Beijing plus 2008

[email protected] programming]$ cat Mysed.txt
Beijing Beijing Beijing Beijing
London London London London
[[Email protected] programming]$ sed ' s/\ (beijing\) \ (. *\) \ (beijing\)/\12008\2\32008/' Mysed.txt
Beijing2008 Beijing Beijing Beijing2008
London London London London

This command is a bit more complicated, with a trick, that is, pre-storage, that is, the matches that are enclosed by \ (and \) are stored sequentially, stored in \1, \2 ... Inside. This allows you to use \ Plus numbers to invoke the content. This example uses this technique to store three of content, respectively, matching Beijing, matching. * and matching Beijing.

You might ask, if I want to put m before the first T, and add n before the second T, how do I do it?

This requires the use of regular expression of non-greedy algorithm, generally in the?, +, *, {n}, {m,}, {m,n} immediately after the next, will make the match from greedy algorithm to non-greedy algorithm, but unfortunately, SED does not support this non-greedy algorithm, so we can not use SED to realize this idea , you can only find another tool. This is also a legacy of the SED problem.

Example seven can use the pattern to locate the row range

[email protected] programming]$ cat Mysed.txt
Beijing 2003
Beijing 2004
Beijing 2005
Beijing 2006
Beijing 2007
Beijing 2008
Beijing 2007
[Email protected] programming]$ sed-n '/2005/,/2007/p ' mysed.txt
Beijing 2005
Beijing 2006
Beijing 2007

We use/2005/to match the first row of the row range, using/2008/to match the last line of the row range. As you can see, when you match a trailing row, you stop, and no longer match backwards, as long as you encounter a line that meets the requirements. (just match to the first 2007, and there is no match to the second 2007)

Example eight using the-e option to set multiple command

Remember the command section, now I'm telling you, sed commands can contain more than one command, but when you include more than one command, you must precede each command with option-E.

[Email protected] programming]$ sed-n-E ' 1,2p '-e ' 4p ' mysed.txt
Beijing 2003
Beijing 2004
Beijing 2006
Although this example seems to use-e in order to use-E, the-e is useful in some cases. Believe me, hehe:)

Remember, the command must be immediately followed by-E, and no additional options are allowed.

Example nine can use-F to set the name of the file containing the command

If your command is long, you can write it in a file and use-F to set the file as the command part:

[email protected] programming]$ cat callsed
/2004/,/2006/p
[Email protected] programming]$ sed-n-F callsed mysed.txt
Beijing 2004
Beijing 2005
Beijing 2006
Two commands explain the use of-F. I believe you can read it.

"Stream Art of the SED III"-linux command five-minute series 23

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.