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

Sed - An Introduction and Tutorial by Bruce Barnett

发布时间:2021-01-25 14:11:33 所属栏目: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;

<tr>
<td colspan="4" align="center">Relations between d,and !</td>
</tr>
<tr>

This table shows that the following commands are identical:

sed -n '1,10 p'
sed -n '11,$ !p'
sed '1,10 !d'
sed '11,$ d'

It also shows that the "!" command "inverts" the address range,operating on the other lines.

There is one more simple command that can restrict the changes to a set of lines. It is the "q" command: quit. the third way to duplicate the head command is:

sed '11 q'

which quits when the eleventh line is reached. This command is most useful when you wish to abort the editing after some condition is reached.

The "q" command is the one command that does not take a range of addresses. Obviously the command

sed '1,10 q'

cannot quit 10 times. Instead

sed '1 q'

or

sed '10 q'

is correct.

The curly braces,"{" and "}," are used to group the commands.

(编辑:晋中站长网)

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

The character "^I" is a?CTRL-I?or tab character. You would have to explicitly type in the tab. Note the order of operations above,which is in that order for a very good reason. Comments might start in the middle of a line,with white space characters before them. Therefore comments are first removed from a line,potentially leaving white space characters that were before the comment. The second command removes all trailing blanks,so that lines that are now blank are converted to empty lines. The last command deletes empty lines. Together,the three commands remove all lines containing only comments,tabs or spaces.

This demonstrates the pattern space?sed?uses to operate on a line. The actual operation?sed?uses is:

  • Copy the input line into the pattern space.
  • Apply the first?sed?command on the pattern space,if the address restriction is true.
  • Repeat with the next sed expression,againoperating on the pattern space.
  • When the last operation is performed,write out the pattern spaceand read in the next line from the input file.

Another useful command is the print command: "p." If?sed?wasn't started with an "-n" option,the "p" command will duplicate the input. The command

sed 'p'

will duplicate every line. If you wanted to double every empty line,use:

sed '/^$/ p'

Adding the "-n" option turns off printing unless you request it. Another way of duplicating?head's functionality is to print only the lines you want. This example prints the first 10 lines:

sed -n '1,10 p' 

Sed?can act like?grep?by combining the print operator to function on all lines that match a regular expression:

sed -n '/match/ p' 

which is the same as:

grep match

Sometimes you need to perform an action on every line except those that match a regular expression,or those outside of a range of addresses. The "!" character,which often means?not?in UNIX utilities,inverts the address restriction. You remember that

sed -n '/match/ p'

acts like the?grep?command. The "-v" option to?grep?prints all lines that don't contain the pattern.?Sed?can do this with

sed -n '/match/ !p' 

As you may have noticed,there are often several ways to solve the same problem with?sed. This is because?print?and?delete?are opposite functions,and it appears that "!p" is similar to "d," while "!d" is similar to "p." I wanted to test this,so I created a 20 line file,and tried every different combination. The following table,which shows the results,demonstrates the difference:

<table border="">

Sed Range Command Results
热点阅读