Sed - An Introduction and Tutorial by Bruce Barnett
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:
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:
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: (编辑:威海站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |