Sed - An Introduction and Tutorial by Bruce Barnett
The "=" command prints the current line number to standard output. One way to find out the line numbers that contain a pattern is to use: # add line numbers first,# then use grep,# then just print the number cat -n file | grep 'PATTERN' | awk '{print $1}' The?sed?solution is: sed -n '/PATTERN/ =' file Earlier I used the following to find the number of lines in a file #!/bin/sh lines=`wc -l file | awk '{print $1}' ` Using the "=" command can simplify this: #!/bin/sh lines=`sed -n '$=' file ` The "=" command only accepts one address,so if you want to print the number for a range of lines,you must use the curly braces: #!/bin/sh # Just print the line numbers sed -n '/begin/,/end/ { = d }' file Since the "=" command only prints to standard output,you cannot print the line number on the same line as the pattern. You need to edit multi-line patterns to do this. If you wanted to change a word from lower case to upper case,you could write 26 character substitutions,converting "a" to "A," etc.?Sed?has a command that operates like the?tr?program. It is called the "y" command. For instance,to change the letters "a" through "f" into their upper case form,use: sed 'y/abcdef/ABCDEF/' file Here's a sed example that convers all uppercase letters to lowercase letters,like the tr command: sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' If you wanted to convert a line that contained a hexadecimal number (e.g. 0x1aff) to upper case (0x1AFF),you could use: sed '/0x[0-9a-zA-Z]*/ y/abcdef/ABCDEF' file This works fine if there are only numbers in the file. If you wanted to change the second word in a line to upper case,and you are using classic sed,you are out of luck - unless you use multi-line editing. (Hey - I think there is some sort of theme here!) However,GNU sed has a uppercase and lowercase extension. The "l" command prints the current pattern space. It is therefore useful in debugging?sed?scripts. It also converts unprintable characters into printing characters by outputting the value in octal preceded by a "" character. I found it useful to print out the current pattern space,while probing the subtleties of?sed. There are three new commands used in multiple-line patterns: "N," "D," and "P." I will explain their relation to the matching "n," and "p" single-line commands. The "n" command will print out the current pattern space (unless the "-n" flag is used),empty the current pattern space,and read in the next line of input. The "N" command does?not?print out the current pattern space and does?not?empty the pattern space. It reads in the next line,but appends a new line character along with the input line itself to the pattern space. The "d" command deletes the current pattern space,reads in the next line,puts the new line into the pattern space,and aborts the current command,and starts execution at the first?sed?command. This is called starting a new "cycle." The "D" command deletes the first portion of the pattern space,up to the new line character,leaving the rest of the pattern alone. Like "d," it stops the current command and starts the command cycle over again. However,it will not print the current pattern space. You must print it yourself,a step earlier. If the "D" command is executed with a group of other commands in a curly brace,commands after the "D" command are ignored. The next group of?sed?commands is executed,unless the pattern space is emptied. If this happens,the cycle is started from the top and a new line is read. The "p" command prints the entire pattern space. The "P" command only prints the first part of the pattern space,up to the NEWLINE character. Neither the "p" nor the "P" command changes the patterns space. Some examples might demonstrate "N" by itself isn't very useful. the filter sed -e 'N' doesn't modify the input stream. Instead,it combines the first and second line,then prints them,combines the third and fourth line,and prints them,etc. It does allow you to use a new "anchor" character: "n." This matches the new line character that separates multiple lines in the pattern space. If you wanted to search for a line that ended with the character "#," and append the next line to it,you could use #!/bin/sh sed ' # look for a "#" at the end of the line /#$/ { # Found one - now read in the next line N # delete the "#" and the new line character,s/#n// }' file You could search for two lines containing "ONE" and "TWO" and only print out the two consecutive lines: #!/bin/sh sed -n ' /ONE/ { # found "ONE" - read in next line N # look for "TWO" on the second line # and print if there. /n.*TWO/ p }' file (编辑:威海站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |