Example 5 insert some content after some strings
[Rocrocket @ rocrocket programming] $ cat mysed.txt
Beijing
London [rocrocket @ rocrocket programming] $ SED's/B. */& 2008/'mysed.txt
Beijing2008
London
The purpose of this command is to add the following four characters to the location of 'B. The & characters are used in this command. This is a skill. These skills are very important for comparison between experts and cainiao. & Indicates the "matched part", which is 'beijing' here!
Example 6 Add 2008 after the first Beijing and 2008 after the last Beijing.
[Rocrocket @ rocrocket programming] $ cat mysed.txt
Beijing
London
[Rocrocket @ rocrocket programming] $ SED's // (Beijing/)/(. */)/(Beijing/) // 12008/2/32008/'mysed.txt
Beijing2008 Beijing Beijing2008
London
This command is a little complicated. One of the techniques used is pre-storage, that is, the matching content enclosed by/(and/) will be stored in order, store to/1,/2... . In this way, you can use/to add numbers to call the content. This example uses this technique to store three contents, matching Beijing, matching. *, and matching Beijing.
You may ask, if I want to add m before the first T and N before the second T, how can I do this?
This requires a non-greedy algorithm using regular expressions? , +, *, {N}, {M,}, {m, n} followed by one ?, This will change matching from greedy algorithms to non-Greedy algorithms. Unfortunately, sed does not support this non-greedy algorithm, so we cannot use sed to implement this idea. We can only find other tools. This is also a legacy issue of sed.
Example 7: You can use the mode to locate the row range.
[Rocrocket @ rocrocket programming] $ cat mysed.txt
Beijing 2003
Beijing 2004
Beijing 2005
Beijing 2006
Beijing 2007
Beijing 2008
Beijing 2007
[Rocrocket @ rocrocket programming] $ sed-n'/2005/,/2007/P' mysed.txt
Beijing 2005
Beijing 2006
Beijing 2007
We use/2005/to match the first row in the row range, And/2008/to match the ending row in the row range. As you can see, when matching the ending line, as long as the line meets the requirements is stopped, it will not continue to backward match. (Only matching the first 2007 does not match the second 2007)
Example 8 use the-E Option to set multiple commands
Remember the command section. Now I tell you that the SED command can contain more than one command, but when it contains more than one command, you must add option-E before each command.
[Rocrocket @ rocrocket programming] $ sed-n-e '1, 2 p '-e '4p' mysed.txt
Beijing 2003
Beijing 2004
Beijing 2006
Although this example seems to use-e to use-E,-E is useful in some cases. Believe me, haha :)
Remember,-e must be followed by command immediately. Other options cannot be included.
Example 9 use-F to set the name of the file containing the command
If your command is very long, you can write it in the file, and then use-F to set this file as the command part:
[Rocrocket @ rocrocket programming] $ cat callsed
/2004/,/2006/p
[Rocrocket @ rocrocket programming] $ sed-n-F callsed mysed.txt
Beijing 2004
Beijing 2005
Beijing 2006
The two commands clearly explain the usage of-f. I believe you can understand it.