Sed - An Introduction and Tutorial by Bruce Barnett
Click here to get file:?You can modify this to print any number of lines around a pattern. As you can see,you must remember what is in the hold space,and what is in the pattern space. There are other ways to write the same routine. Instead of exchanging the hold space with the pattern space,you can copy the hold space to the pattern space with the "g" command. This deletes the pattern space. If you want to append to the pattern space,use the "G" command. This adds a new line to the pattern space,and copies the hold space after the new line. Here is another version of the "grep3" command. It works just like the previous one,but is implemented differently. This illustrates that?sed?has more than one way to solve many problems. What is important is you understand your problem,and document your solution: #!/bin/sh # grep3 version c: use 'G' instead of H Click here to get file:? The "G" command makes it easy to have two copies of a line. Suppose you wanted to the convert the first hexadecimal number to uppercase,and don't want to use the script I described in an earlier column? #!/bin/sh # change the first hex number to upper case format # uses sed twice # used as a filter # convert2uc Click here to get file:? Here is a solution that does not require two invocations of?sed:? #!/bin/sh # convert2uc version b # change the first hex number to upper case format # uses sed once # used as a filter # convert2uc Click here to get file:?Carl Henrik Lunde suggested a way to make this simpler. I was working too hard.? #!/bin/sh # convert2uc version b # change the first hex number to upper case format # uses sed once # used as a filter # convert2uc Click here to get file:?This example only converts the letters "a" through "f" to upper case. This was chosen to make the script easier to print in these narrow columns. You can easily modify the script to convert all letters to uppercase,or to change the first letter,second word,etc. As you learn about?sed?you realize that it has its own programming language. It is true that it's a very specialized and simple language. What language would be complete without a method of changing the flow control? There are three commands?sed?uses for this. You can specify a label with an text string preceded by a colon. The "b" command branches to the label. The label follows the command. If no label is there,branch to the end of the script. The "t" command is used to test conditions. Before I discuss the "t" command,I will show you an example using the "b" command. This example remembers paragraphs,and if it contains the pattern (specified by an argument),the script prints out the entire paragraph.? #!/bin/sh sed -n ' # if an empty line,check the paragraph /^$/ b para # else add it to the hold buffer H # at end of file,check paragraph $ b para # now branch to end of script b # this is where a paragraph is checked for the pattern :para # return the entire paragraph # into the pattern space x # look for the pattern,if there - print /'$1'/ p ' Click here to get file:? (编辑:威海站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |