加入收藏 | 设为首页 | 会员中心 | 我要投稿 网站开发网_马鞍山站长网 (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

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 

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

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