Sed - An Introduction and Tutorial by Bruce Barnett
|
The "x" command exchanges the hold buffer and the pattern buffer. Both are changed. The "h" command copies the pattern buffer into the hold buffer. The pattern buffer is unchanged. An identical script to the above uses the hold commands: #!/bin/sh # grep3 version b - another version using the hold commands # if there is only one argument,exit Click here to get file:? The "H" command allows you to combine several lines in the hold buffer. It acts like the "N" command as lines are appended to the buffer,with a "n" between the lines. You can save several lines in the hold buffer,and print them only if a particular pattern is found later. As an example,take a file that uses spaces as the first character of a line as a continuation character. The files?/etc/termcap,?/etc/printcap,?makefile?and mail messages use spaces or tabs to indicate a continuing of an entry. If you wanted to print the entry before a word,you could use this script. I use a "^I" to indicate an actual tab character: #!/bin/sh
# print previous entry
sed -n '
/^[ ^I]/!{
# line does not start with a space or tab,# does it have the pattern we are interested in?
'/$1/' {
# yes it does. print three dashes
i
---
# get hold buffer,save current line
x
# now print what was in the hold buffer
p
# get the original line back
x
}
# store it in the hold buffer
h
}
# what about lines that start
# with a space or tab?
/^[ ^I]/ {
# append it to the hold buffer
H
}'
Click here to get file:? You can also use the "H" to extend the context grep. In this example,the program prints out the two lines before the pattern,instead of a single line. The method to limit this to two lines is to use the "s" command to keep one new line,and deleting extra lines. I call it?grep4: #!/bin/sh (编辑:网站开发网_马鞍山站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |

