The output would be
ONE two three,one two three
four three two ONE
ONE hundred
Note that this changed "one" to "ONE" once on each line. The first line had "one" twice,but only the first occurrence was changed. That is the default behavior. If you want something different,you will have to use some of the options that are available. I'll explain them later.
So let's continue.
There are four parts to this substitute command:
s Substitute command
/../../ Delimiter
one Regular Expression Pattern Search Pattern
ONE Replacement string
The search pattern is on the left hand side and the replacement string is on the right hand side.
We've covered??and?. That's 90% of the effort needed to learn the substitute command. To put it another way,you already know how to handle 90% of the most frequent uses of?sed.?There are a ... few fine points that any future sed expert should know about. (You just finished section 1. There are only 63 more sections to cover. :-) Oh. And you may want to bookmark this page,.... just in case you don't finish.
The character after the?s?is the delimiter. It is conventionally a slash,because this is what?ed,?more,and?vi?use. It can be anything you want,however. If you want to change a pathname that contains a slash - say /usr/local/bin to /common/bin - you could use the backslash to quote the slash:
sed 's//usr/local/bin//common/bin/' new
Gulp. Some call this a 'Picket Fence' and it's ugly. It is easier to read if you use an underline instead of a slash as a delimiter:
sed 's_/usr/local/bin_/common/bin_' new
Some people use colons:
sed 's:/usr/local/bin:/common/bin:' new
Others use the "|" character.
sed 's|/usr/local/bin|/common/bin|' new
Pick one you like. As long as it's not in the string you are looking for,anything goes. And remember that you need three delimiters. If you get a "Unterminated `s' command" it's because you are missing one of them.
Sometimes you want to search for a pattern and add some characters,like parenthesis,around or near the pattern you found. It is easy to do this if you are looking for a particular string:
sed 's/abc/(abc)/' new
This won't work if you don't know exactly what you will find. How can you put the string you found in the replacement string if you don't know what it is?
The solution requires the special character "&." It corresponds to the pattern found.
sed 's/[a-z]*/(&)/' new
You can have any number of "&" in the replacement string. You could also double a pattern,e.g. the first number of a line:
% echo "123 abc" | sed 's/[0-9]*/& &/'
123 123 abc
Let me slightly amend this example. Sed will match the first string,and make it as greedy as possible. I'll cover that later. If you don't want it to be so greedy (i.e. limit the matching),you need to put restrictions on the match.
The first match for '[0-9]*' is the first character on the line,as this matches zero of more numbers. So if the input was "abc 123" the output would be unchanged (well,except for a space before the letters). A better way to duplicate the number is to make sure it matches a number:
% echo "123 abc" | sed 's/[0-9][0-9]*/& &/'
123 123 abc
(编辑:PHP编程网 - 黄冈站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!