dslreports logo
site
 
    All Forums Hot Topics Gallery
spc

spacer




how-to block ads


Search Topic:
share rss forum feed

pablo
MVM
join:2003-06-23
kudos:1
reply to Brano

Re: grep (awk, sed) help

Hi Brano,

As always, Linux/Unix has many filters to solve the same issue. :)

Here's a solution using sed.

Suppose we have the following text file ....

BOO
a
b
BOO
x
y
z
BOO
black
blue
green
BOO
 

The following "sed" script would remove BOO and the preceding line:

sed code:
#
# Handle the special case when our token is on line 1.
#
/^BOO$/d

#
# This is the generic case for line 2 .. N
#
/^.*$/{
N
/.*\nBOO$/d
P
D
}

--
openSUSE 12.1/KDE 4.x
ISP: TekSavvy Bonded DSL; backhauled via a 6KM wireless link
Assorted goodies: »pablo.blog.blueoakdb.com


Brano
I hate Vogons
Premium,MVM
join:2002-06-25
Burlington, ON
kudos:11
Reviews:
·TekSavvy DSL
·Bell Fibe

1 edit
Thank you both. I was able to do what I need using multiple line script, but was hoping for simpler potentially one line solution. ... Pablo, your's the one I guess

FYI: My pattern is never on 1st line so the generic sed should just do perfectly, I'll test it later.

pablo
MVM
join:2003-06-23
kudos:1
Howdy,

You're welcome ... as a gentle reminder, invoke the sed script using the `-n' parameter: sed -n -f the-script-file file-1 ... file-N



Cheers!
-pablo
--
openSUSE 12.2/KDE 4.x
ISP: TekSavvy Bonded DSL; backhauled via a 6KM wireless link
Assorted goodies: »pablo.blog.blueoakdb.com