Operating System - HP-UX
1830862 Members
2607 Online
110017 Solutions
New Discussion

Re: grep and fetch previous line

 
uform
Frequent Advisor

grep and fetch previous line

Sample Data:

Line 1: pro_fw_tssndmstats_cuyr_pri_f
Line 2: 08/11/2006 23:22 08/12/2006 00:16 OH Line 3: 10335470/1

Line 1: pro_fw_tssndmstats_cuyr_pri_f
Line 2: 08/11/2006 23:22 08/12/2006 00:16 OH Line 3: 10335470/1

Now i want to find lines those has word 'OH'
in it.

I'm using grep ' OH '. It returns only
line 2, Fine. But i'm interested in knowing the job name which is available only in Line 1.

How to grep for a word and fetch the previous
line from there ???

Thanks
9 REPLIES 9
Victor Fridyev
Honored Contributor

Re: grep and fetch previous line

Hi,

I'm not sure that you can do this with grep, but it's easy to do with awk:

awk '$5=="OH" {print prev;next}
{prev=$0}' datafile

HTH
Entities are not to be multiplied beyond necessity - RTFM
A. Clay Stephenson
Acclaimed Contributor

Re: grep and fetch previous line

There are many ways to do this but grep is a poor choice in this case. I'll do this in awk but you could do it in sed or Perl quite easily.

Create an awk file, my.awk that looks something like this:
------------------------
BEGIN {
prev = "No Previous line!"
}
{
if ($0 ~ "OH$")
{
print prev
print $0
}
prev = $0
}
------------------------
You then execute it like this:
awk -f my.awk < infile > outfile

Note that I am using an anchored string and only matching "OH" when found at the end of the line; that's what the '$' does in "OH$".

The nice thing about using awk for this is that using the posiotional variables is very easy.
For example, if the current line is this:
08/11/2006 23:22 08/12/2006 00:16 OH
$0 = the entire line
$1 = 08/11/2006
$2 = 23:22
$3 = 08/12/2006
$4 = 00:16
$5 = $NF = OH
NF (Number of fields) = 5

This should make extracting exactly what you want, very easy.

If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: grep and fetch previous line

Hi:

# perl -nle 'BEGIN{$pat=shift;die unless @ARGV};print $last,"\n",$_ if m/$pat/io;$last=$_' pattern filename

Regards!

...JRF...
Sandman!
Honored Contributor

Re: grep and fetch previous line

Yet another way to do the same thing with ex(1):

# ex -s +"g/OH/-1,/OH/p | q!" inputfile

cheers!
uform
Frequent Advisor

Re: grep and fetch previous line

ok ..its my fault i didnt make my requirement clear.

1) OH may not be always at the end of line 2.
but will be in some where in line 2.

how should i pass input to awk?
A. Clay Stephenson
Acclaimed Contributor

Re: grep and fetch previous line

In the case of my example, change:
if ($0 ~ "OH$")
to:
if ($0 ~ "OH")

"OH" will then be matched anywhere in the line.
If it ain't broke, I can fix that.
Victor Fridyev
Honored Contributor

Re: grep and fetch previous line

With awk:

awk '/OH/ {print prev;next}
{prev=$0}' datafile
HTH
Entities are not to be multiplied beyond necessity - RTFM
Sandman!
Honored Contributor

Re: grep and fetch previous line

...and here's how to do the same thing with sed

# sed -n '/^Line 1:/N;/OH/p' inputfile

~cheers
John_Hancock
Trusted Contributor

Re: grep and fetch previous line

I know this will not answer your question but I will tell you what we solved the same problem.

We wanted the process name of a failed cron job. In this case we could get the PID by a simple logfile parse (in our case from OVO). However it was a little more difficult to get the actual name of the process that failed. To get this we had to find the second last reference to that process id and fetch the previous line.

Because the processing was done soon after the lines were added, and cron logs could be megs long, and we wanted to minimise the system load I ended up gatting a c programmer to use a reverse reading routine that he wrote to which I could pass the process id and it would return the line required. It rurned out to be extremely fast and efficient.