加入收藏 | 设为首页 | 会员中心 | 我要投稿 网站开发网_马鞍山站长网 (https://www.0555zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

Sed - An Introduction and Tutorial by Bruce Barnett

发布时间:2021-01-29 10:07:16 所属栏目:站长百科 来源:网络整理
导读:副标题#e# 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

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

}
'/$1/' {

matches - append the current line

H
# get the next line
n
# append that one also
H
# bring it back,but keep the current line in
# the hold buffer. This is the line after the pattern,# and we want to place it in hold in case the next line
# has the desired pattern
x
# print the 4 lines
p
# add the mark
a

}'

(编辑:网站开发网_马鞍山站长网)

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