republican-creole
site Search:


 
    All Forums Hot Topics Gallery






how-to block ads


 
Search Topic:
Uniqs:
501
Share Topic
Posting?
Post a:
Post a:
Links: ·Forum FAQ ·Attitude Adjustment ·Linux docs ·DistroWatch ·OPLM
AuthorAll Replies


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

grep (awk, sed) help

I have a text file with various text on separate lines. How can I match on pattern and then not display that line and line immediately before the matched line?

For the one line I can do grep -v mypattern myfile ... works for the one line.
I can match the previous line with grep -B1 mypattern myfile ... but how can I combine these two expressions?

If line is matching mypattern don't output the line and don't output the line just before the matched line.

I'd like to stay with shell tools i.e. grep, awk, sed (no perl, no python).

Thank you!!!


Black Box

join:2002-12-21

3 edits

This looks like a job for awk. Here is my first crack at it, I bet it can be improved:

awk code:

#!/usr/bin/awk -f                                                             

BEGIN {
prev = ""
skip = "YES"
}

{
if (/mypattern/) {
skip = "YES"
} else {
if ("NO" == skip) {
print prev
}

prev = $0
skip = "NO"
}
}

END {
if ("NO" == skip) {
print prev
}
}

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

reply to Brano
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:6
Reviews:
·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


Uraki
Uraki
Premium
join:2003-06-22
Belle Plaine, KS

reply to Brano
»www.theunixschool.com/2012/06/se···-or.html

Check out #23 in the list, exactly what you want, I think.



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

Ok guys, thank you for all 3 examples.

The awk posted by Black Box See Profile and the sed posted by Uraki See Profile are doing what I need.
pablo See Profile your is not producing desired output (or I'm doing something wrong).

In any case, mission accomplished, thank you all for help.


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

Did you try invoke sed as I mentioned in my last post? "sed -n -f the-script-file input-file-1 input-file-2 ... etc"

Thx!
-pablo



Black Box

join:2002-12-21

reply to Brano
For the sake of completeness, I'll mention that the whole script can be put in a file (let's say myfilter) that if made executable can be executed directly. I takes the same parameters as awk, so if invoked myfilter input.txt it will filter the input.txt file and put the filtered output on the standard output. Pipe or redirect as you wish ;).

--
Keep It Safe, Stupid!
Yes, I CanChat. Can You?


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

Funny you mention that Black Box See Profile as offline, I mentioned the same Brano See Profile ...

Incidentally, the issue why my sed script wasn't working was resolved. The Regular Expression Brano See Profile used didn't cover the input file.

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


Monday, 20-May 17:25:47 Terms of Use & Privacy | feedback | contact | Hosting by nac.net - DSL,Hosting & Co-lo
over 13.5 years online © 1999-2013 dslreports.com.
Most commented news this week
Hot Topics