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

I have mentioned the pattern space before. Most commands operate on the pattern space,and subsequent commands may act on the results of the last modification. The three previous commands,like the read file command,add the new lines to the output stream,bypassing the pattern space.

You may remember in my last tutorial I warned you that some commands can take a range of lines,and others cannot. To be precise,the commands "a," "i," "r," and "q" will not take a range like "1,100" or "/begin/,/end/." The documentation states that the read command can take a range,but I get an error when I try this. The "c" or change command allows this,and it will let you change several lines into one:

#!/bin/sh
sed '
/begin/,/end/ c
***DELETED***
'

If you need to do this,you can use the curly braces,as that will let you perform the operation on every line:

#!/bin/sh
# add a blank line after every line
sed '1,$ {
    a

}'

Most UNIX utilities are line oriented. Regular expressions are line oriented. Searching for patterns that covers more than one line is not an easy task. (Hint: It will be very shortly.)

Sed?reads in a line of text,performs commands which may modify the line,and outputs modification if desired. The main loop of a?sed?script looks like this:

  1. The next line is read from the input file and places it in the pattern space. If the end of file is found,and if there are additional files to read,the current file is closed,the next file is opened,and the first line of the new file is placed into the pattern space.
  2. The line count is incremented by one. Opening a new file does not reset this number.
  3. Each?sed?command is examined. If there is a restriction placed on the command,and the current line in the pattern space meets that restriction,the command is executed. Some commands,like "n" or "d" cause?sed?to go to the top of the loop. The "q" command causes?sed?to stop. Otherwise the next command is examined.
  4. After all of the commands are examined,the pattern space is output unless?sed?has the optional "-n" argument.

The restriction before the command determines if the command is executed. If the restriction is a pattern,and the operation is the delete command,then the following will delete all lines that have the pattern:

/PATTERN/ d

If the restriction is a pair of numbers,then the deletion will happen if the line number is equal to the first number or greater than the first number and less than or equal to the last number:

10,20 d

If the restriction is a pair of patterns,there is a variable that is kept for each of these pairs. If the variable is false and the first pattern is found,the variable is made true. If the variable is true,the command is executed. If the variable is true,and the last pattern is on the line,after the command is executed the variable is turned off:

/begin/,/end/ d

Whew! That was a mouthful. If you have read carefully up to here,you should have breezed through this. You may want to refer back,because I covered several subtle points. My choice of words was deliberate. It covers some unusual cases,like:

# what happens if the second number
# is less than the first number?
sed -n '20,1 p' file

and

# generate a 10 line file with line numbers
# and see what happens when two patterns overlap
yes | head -10 | cat -n | 
sed -n -e '/1/,/7/ p' -e '/5/,/9/ p'

Enough mental punishment. Here is another review,this time in a table format. Assume the input file contains the following lines:

AB
CD
EF
GH
IJ

When?sed?starts up,the first line is placed in the pattern space. The next line is "CD." The operations of the "n," "d," and "p" commands can be summarized as:

Pattern Space The "n" command may or may not generate output depending upon the existence of the "-n" flag.

That review is a little easier to follow,isn't it? Before I jump into multi-line patterns,I wanted to cover three more commands:

(编辑:晋中站长网)

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

Next Input Command Output New Pattern Space New Text Input