加入收藏 | 设为首页 | 会员中心 | 我要投稿 网站开发网_马鞍山站长网 (https://www.0555zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

Sed - An Introduction and Tutorial by Bruce Barnett

发布时间:2021-01-29 10:07:16 所属栏目:站长百科 来源:网络整理
导读:副标题#e# 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

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.

(编辑:网站开发网_马鞍山站长网)

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