加入收藏 | 设为首页 | 会员中心 | 我要投稿 新余站长网 (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;

With no flags,the first matched substitution is changed. With the "g" option,all matches are changed. If you want to modify a particular pattern that is not the first one on the line,you could use "(" and ")" to mark each pattern,and use "1" to put the first pattern back unchanged. This next example keeps the first word on the line but deletes the second:

sed 's/([a-zA-Z]*) ([a-zA-Z]*) /1 /' new

Yuck. There is an easier way to do this. You can add a number after the substitution command to indicate you only want to match that particular pattern. Example:

sed 's/[a-zA-Z]* //2' new

You can combine a number with the g (global) flag. For instance,if you want to leave the first word alone,but change the second,third,etc. to be DELETED instead,use /2g:

sed 's/[a-zA-Z]* /DELETED /2g' new

Don't get /2 and 2 confused. The /2 is used at the end. 2 is used in inside the replacement field.

Note the space after the "*" character. Without the space,?sed?will run a long,long time. (Note: this bug is probably fixed by now.) This is because the number flag and the "g" flag have the same bug. You should also be able to use the pattern

sed 's/[^ ]*//2' new

but this also eats CPU. If this works on your computer,and it does on some UNIX systems,you could remove the encrypted password from the password file:

sed 's/[^:]*//2' /etc/password.new

But this didn't work for me the time I wrote this. Using "[^:][^:]*" as a work-around doesn't help because it won't match an non-existent password,and instead delete the third field,which is the user ID! Instead you have to use the ugly parenthesis:

sed 's/^([^:]*):[^:]:/1::/'  /etc/password.new

You could also add a character to the first pattern so that it no longer matches the null pattern:

sed 's/[^:]*:/:/2'  /etc/password.new

The number flag is not restricted to a single digit. It can be any number from 1 to 512. If you wanted to add a colon after the 80th character in each line,you could type:

sed 's/./&:/80' new

You can also do it the hard way by using 80 dots:

sed 's/^................................................................................/&:/' new

By default,?sed?prints every line. If it makes a substitution,the new text is printed instead of the old one. If you use an optional argument to sed,"sed -n," it will not,print any new lines. I'll cover this and other options later. When the "-n" option is used,the "p" flag will cause the modified line to be printed. Here is one way to duplicate the function of?grep?with?sed:

sed -n 's/pattern/&/p' 

But a simpler version is described?

There is one more flag that can follow the third delimiter. With it,you can specify a file that will receive the modified data. An example is the following,which will write all lines that start with an even number,followed by a space,to the file?even:?

sed -n 's/^[0-9]*[02468] /&/w even' 

In this example,the output file isn't needed,as the input was not modified. You must have exactly one space between the?w?and the filename. You can also have ten files open with one instance of?sed. This allows you to split up a stream of data into separate files. Using the previous example combined with multiple substitution commands described later,you could split a file into ten pieces depending on the last digit of the first number. You could also use this method to log error or debugging information to a special file.

GNU has added another pattern flags - /I

This flag makes the pattern match case insensitive. This will match abc,aBc,ABC,AbC,etc.:

sed '/abc/I' new

You can combine flags when it makes sense. Please note that the "w" has to be the last flag. For example the following command works:

sed -n 's/a/A/2pw /tmp/file' new

Next I will discuss the options to?sed,and different ways to invoke?sed.

previously,I have only used one substitute command. If you need to make two changes,and you didn't want to read the manual,you could pipe together multiple?sedcommands:

sed 's/BEGIN/begin/' new

This used two processes instead of one. A?sed?guru never uses two processes when one can do.

One method of combining multiple commands is to use a?-e?before each command:

sed -e 's/a/A/' -e 's/b/B/' new

(编辑:新余站长网)

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

热点阅读