加入收藏 | 设为首页 | 会员中心 | 我要投稿 晋中站长网 (https://www.0354zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Sed - An Introduction and Tutorial by Bruce Barnett

发布时间:2021-01-25 14:11:33 所属栏目:Linux 来源:网络整理
导读:http://www.grymoire.com/unix/sed.html Quick Links - NEW table border="1" tr Sed Pattern Flags /tr tr td a href="http://www.grymoire.com/unix/Sed.html#uh-6"gt;/g?- Global/td /tr tr td a href="http://www.grymoire.com/unix/Sed.html#uh-10a"gt;

One use of the hold buffer is to remember previous lines. An example of this is a utility that acts like?grep?as it shows you the lines that match a pattern. In addition,it shows you the line before and after the pattern. That is,if line 8 contains the pattern,this utility would print lines 7,8 and 9.

One way to do this is to see if the line has the pattern. If it does not have the pattern,put the current line in the hold buffer. If it does,print the line in the hold buffer,then the current line,and then the next line. After each set,three dashes are printed. The script checks for the existence of an argument,and if missing,prints an error. Passing the argument into the?sed?script is done by turning off the single quote mechanism,inserting the "$1" into the script,and starting up the single quote again:

#!/bin/sh
# grep3 - prints out three lines around pattern
# if there is only one argument,exit

case $# in
1);;
*) echo "Usage: $0 pattern";exit;;
esac;

I hope the argument doesn't contain a /

if it does,sed will complain

use sed -n to disable printing

unless we ask for it

sed -n '
'/$1/' !{

no match - put the current line in the hold buffer

x
# delete the old one,which is 
# now in the pattern buffer
d

}
'/$1/' {

a match - get last line

x
# print it
p
# get the original line back
x
# print it
p
# get the next line 
n
# print it
p
# now add three dashes as a marker
a

# now put this line into the hold buffer
x

}'

Click here to get file:?

You could use this to show the three lines around a keyword,i.e.:

grep3 vt100 

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

case $# in
1);;
*) echo "Usage: $0 pattern";exit;;
esac;

again - I hope the argument doesn't contain a /

use sed -n to disable printing

sed -n '
'/$1/' !{

put the non-matching line in the hold buffer

h

}
'/$1/' {

found a line that matches

# append it to the hold buffer
H
# the hold buffer contains 2 lines
# get the next line
n
# and add it to the hold buffer
H
# now print it back to the pattern space
x
# and print it.
p
# add the three hyphens as a marker
a

}'

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

grep4: prints out 4 lines around pattern

if there is only one argument,exit

case $# in
1);;
*) echo "Usage: $0 pattern";exit;;
esac;

sed -n '
'/$1/' !{

does not match - add this line to the hold space

H
# bring it back into the pattern space
x
# Two lines would look like .*n.*
# Three lines look like .*n.*n.*
# Delete extra lines - keep two
s/^.*n(.*n.*)$/1/
# now put the two lines (at most) into 
# the hold buffer again
x

(编辑:晋中站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读