Print 4th line back from regexp

I'm looking for a way to print the 4th line back from a regular expression. Kind of like the below but it has to be the 4th line before the regexp.

  • Print the line immediately before regexp, but not the line containing the regexp.
sed -n '/regexp/{g;1!p;};h'

here is an example of logs(i chopped the logs bc each line is too long)

01:59:09,260 DEBUG SOAP REQUEST: <SOAP-ENV:..<ns1:mdn>NPANXX<
/ns1:mdn>
01:59:09,260 INFO  [com
01:59:09,273 INFO  [org.apache
01:59:09,283 INFO  [stdout]
01:59:29,780 WARNING SendProv has thrown exception

I'm looking for "SendProv has thrown exception" and want to parse the MDN where it threw the exception.

Any ideas?

Try:

awk '{a[NR]=$0;a[NR-5]=""}/SendProv has thrown exception/{print a[NR-4]}' file
sed -n -e '/SendProv has thrown exception/{p;g;p;}' -e 's/.*\(mdn>[^>]*<\).*/\1/;ta' -e 'b' -e ':a' -e 'h'

---------- Post updated at 08:46 PM ---------- Previous update was at 08:35 PM ----------

Only save and print the mdn line is quite easy

sed -n -e '/SendProv has thrown exception/{g;p;}' -e '/mdn>/h'

here are the commands i used to accomplish this. not sure if i could have done it with one

Extract the 4th line above SendProv has thrown exception

awk '{lines[NR] = $0} /SendProv has thrown exception/ {print lines [NR-4]} {delete lines[NR-4]}' server.log>>myfile

extract only the section of MDN

awk -F'mdn' '{for(i = 2; i < NF; i += 2) print $i}' myfile>>myfile1

results:

>NPANXX8854</ns1:

extract just NPANXX1234(this is the actual MDN)

cut -c2-11 myfile1>>myfile2

results:

NPANXX8854

remove duplicates

awk '!x[$0]++' myfile2>>myfile3

Try this:

awk '{TMP[NR%5]=$0} /SendProv/ {FS="[<>]"; $0=TMP[(NR+1)%5]; print $4}' file
NPANXX8845

You may have to add the duplicate removal code...

I'm sure this has been solved already given the tag but is you are looking to resolve the line
1:59:09,260 DEBUG SOAP REQUEST: <SOAP-ENV:..<ns1:mdn>NPANXX<

Simply use the command: grep -B5 "SendProv" testing.txt | head -1

Non-linux users are unlikely to have -B.

Hi.

My experience is different from that.

According to http://en.wikipedia.org/wiki/Usage\_share\_of\_operating_systems , the non-MS-Windows Desktop OSs are (in order) OS-X, Linux, and other.

I have found either a GNU-grep or a GNU work-alike on:

OS-X
BSD {free,new,open}
Solaris
OpenIndiania

I will admit that I don't recall if I installed the GNU code in Solaris and OI, but it must not have been much work if I did. The codes are in very differently-named directories, often not in default path settings, so I would not be surprised that many people do not know about them (and their associated man pages).

Virtually all supercomputers use Linux (ibid.). When I worked in one of the supercomputer centers, the initial Cray-2 used a variant of AT&T Unix, but these days most have shifted over to a form -- often custom -- of Linux. Similar for the ETA10. The TMC TM-5 used Unix in all the nodes as I recall. The 3090s used Unix in VMs (too long ago, but possibly AIX).

Of the "other", I looked at AIX and HP-UX. The AIX7 to which I had access offered GNU grep. I could not find it on HP B.11.11.

The mainframe OSs are usually whatever IBM has for the OS/390 follow-ons, z/OS, but also Linux and openSolaris.

CPAN has a number of grep work-alikes (written in perl, available for copying into one's own home-bin, not needed to install in the system), for example:
http://www.cpan.org/authors/id/A/AD/ADAVIES/peg-3.10 which has the context-printing capability (-B, as well as -A and -C). This code, peg, was quickly made to work on HP-UX, and:

peg -B1 string file

worked as one would expect from GNU grep.

Best wishes ... cheers, drl

( Edit 1: minor typo )

try this:

awk -F"mdn" "{a[NR]=$0}/WARNING/{$0=a[NR-4];print substr($2,2,9)}" file | sort | uniq

*shrug* I don't know what to tell you then, since a very frequent use of awk is reinventing -B for all the posters here which don't happen to have it. Perhaps we don't get an "average" distribution of people, or perhaps "average" is a bad metric to use (how many people here manage *super*computers?) etc.

Hi.

See thread http://www.unix.com/showthread.php?p=302876589\#post302876589 for a contiuation of this sub-topic ... cheers,drl

1 Like

The ex utility fits the bill in case you'd consider it...

ex -sc "/SendProv has thrown exception/-4 | q!" your_log_file

Only caveat here is that the desired line has to be exactly 4 lines before the supplied regexp...