加入收藏 | 设为首页 | 会员中心 | 我要投稿 新余站长网 (https://www.0790zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Sed - An Introduction and Tutorial by Bruce Barnett

发布时间:2021-01-29 08:45:10 所属栏目:Linux 来源:网络整理
导读:http://www.grymoire.com/unix/sed.html Quick Links - NEW table border="1" tr Sed Pattern Flags /tr tr td a href="http://www.grymoire.com/unix/Sed.html#uh-6"gt;/g?- Global/td /tr tr td a href="http://www.grymoire.com/unix/Sed.html#uh-10a"gt;

Do not fret!?It is not your fault you don't understand?sed. I will cover?sed?completely. But I will describe the features in the order that I learned them. I didn't learn everything at once. You don't need to either.

Sed?has several commands,but most people only learn the substitute command:?s. The substitute command changes all occurrences of the regular expression into a new value. A simple example is changing "day" in the "old" file to "night" in the "new" file:

sed s/day/night/ new

Or another way (for UNIX beginners),

sed s/day/night/ old >new

and for those who want to test this:

echo day | sed s/day/night/ 

This will output "night".

I didn't put quotes around the argument because this example didn't need them. If you read my earlier tutorial?,you would understand why it doesn't need quotes. However,I recommend you do use quotes. If you have meta-characters in the command,quotes are necessary. And if you aren't sure,it's a good habit,and I will henceforth quote future examples to emphasize the "best practice." Using the strong (single quote) character,that would be:

sed 's/day/night/' new

I must emphasize that the sed editor changes exactly what you tell it to. So if you executed

echo Sunday | sed 's/day/night/'

This would output the word "Sunnight" because sed found the string "day" in the input.

Another important concept is that sed is line oriented. Suppose you have the input file:

one two three,one two three
four three two one
one hundred

and you used the command

sed 's/one/ONE/' 

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

(编辑:新余站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读