Sed - An Introduction and Tutorial by Bruce Barnett
Here is a way to look for the string "skip3",and if found,delete that line and the next two lines. #!/bin/sh sed '/skip3/ { N N s/skip3n.*n.*/# 3 lines deleted/ }' Note that it doesn't matter what the next two lines are. If you wanted to match 3 particular lines,it's a little more work. This script looks for three lines,where the first line contains "one",the second contained "two" and the third contains "three",replace them with the string "1+2+3": #!/bin/sh sed ' /one/ { N /two/ { N /three/ { N s/onentwonthree/1+2+3/ } } } ' You can either search for a particular pattern on two consecutive lines,or you can search for two consecutive words that may be split on a line boundary. The next example will look for two words which are either on the same line or one is on the end of a line and the second is on the beginning of the next line. If found,the first word is deleted: #!/bin/sh sed ' /ONE/ { # append a line N # "ONE TWO" on same line s/ONE TWO/TWO/ # "ONE # TWO" on two consecutive lines s/ONEnTWO/TWO/ }' file Let's use the"D" command,and if we find a line containing"TWO" immediately after a line containing"ONE," then delete the first line: #!/bin/sh sed ' /ONE/ { # append a line N # if TWO found,delete the first line /n.*TWO/ D }' file Click here to get file:? If we wanted to print the first line instead of deleting it,and not print every other line,change the "D" to a "P" and add a "-n" as an argument to?sed:? #!/bin/sh sed -n ' # by default - do not print anything /ONE/ { # append a line N # if TWO found,print the first line /n.*TWO/ P }' file Click here to get file:? It is very common to combine all three multi-line commands. The typical order is "N," "P" and lastly "D." This one will delete everything between "ONE" and "TWO" if they are on one or two consecutive lines:? #!/bin/sh sed ' /ONE/ { # append the next line N # look for "ONE" followed by "TWO" /ONE.*TWO/ { # delete everything between s/ONE.*TWO/ONE TWO/ # print P # then delete the first line D } }' file Click here to get file:? Earlier I talked about the "=" command,and using it to add line numbers to a file. You can use two invocations of?sed?to do this (although it is possible to do it with one,but that must wait until next section). The first?sed?command will output a line number on one line,and then print the line on the next line. The second invocation of?sed?will merge the two lines together:? #!/bin/sh sed '=' file | sed '{ N s/n/ / }' Click here to get file:? If you find it necessary,you can break one line into two lines,edit them,and merge them together again. As an example,if you had a file that had a hexadecimal number followed by a word,and you wanted to convert the first word to all upper case,you can use the "y" command,but you must first split the line into two lines,change one of the two,and merge them together. That is,a line containing 0x1fff table2 will be changed into two lines: 0x1fff table2 and the first line will be converted into upper case. I will use?tr?to convert the space into a new line: #!/bin/sh tr ' ' ' 12' file| sed ' { y/abcdef/ABCDEF/ N s/n/ / }' Click here to get file:? It isn't obvious,but?sed?could be used instead of?tr. You can embed a new line in a substitute command,but you must escape it with a backslash. It is unfortunate that you must use "n" in the left side of a substitute command,and an embedded new line in the right hand side. Heavy sigh. Here is the example: #!/bin/sh sed ' s/ / /' | sed ' { y/abcdef/ABCDEF/ N s/n/ / }' Click here to get file:? Sometimes I add a special character as a marker,and look for that character in the input stream. When found,it indicates the place a blank used to be. A backslash is a good character,except it must be escaped with a backslash,and makes the?sed?script obscure. Save it for that guy who keeps asking dumb questions. The?sedscript to change a blank into a "" following by a new line would be:?#!/bin/shsed 's/ //' file Click here to get file:? Yeah. That's the ticket. Or use the C shell and really confuse him!?#!/bin/csh -fsed 's/ /\/' file Click here to get file:? (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |