1833582 Members
3897 Online
110061 Solutions
New Discussion

Search Pattern question

 
Vasudevan MV
Frequent Advisor

Search Pattern question

Hello experts,

I have a file like this :
12
13
15
Dest
17
.
.
66
Dest
88
How do I extract/search the word Dest as well as the previous number say for example 15,Dest like that 66,Dest?.
11 REPLIES 11
Sandip Ghosh
Honored Contributor

Re: Search Pattern question

Press Esc :/Dest

Sandip
Good Luck!!!
A. Clay Stephenson
Acclaimed Contributor

Re: Search Pattern question

Here is one simple awk method:

Create an awk file, e.g. my.awk
------------
BEGIN {
prev = ""
}
{
if ($0 ~ /Dest/)
{
printf("%s,%s\n",prev,$0)
prev = ""
}
else prev = $0
}
------------

Then cat yourfile | awk -f my.awk > outfile
If it ain't broke, I can fix that.
H.Merijn Brand (procura
Honored Contributor

Re: Search Pattern question

# perl -ne 'BEGIN{$/="Dest\n"}s,.*\b(\d+)\n(?=$/),$1 ,s&&print' infile
15 Dest
66 Dest
#
Enjoy, Have FUN! H.Merijn
Vasudevan MV
Frequent Advisor

Re: Search Pattern question

Hello all,

Thanks for everyone, Clay's solution worked well and I have already written a shell script for this but I thought there should be some simple command to search multi pattern. Is there any other command or options in grep?

I haven't installed perl on my box.

Thanks
Vasu
H.Merijn Brand (procura
Honored Contributor

Re: Search Pattern question

http://hpux.cs.utah.edu/hppd/hpux/Languages/perl-5.6.1/

When you get used to perl, you can forget all about grep, awk, sed, sh, ...

Well, you /could/, but you shouldn't :) There's a good tool for every problem, it only happens that perl fits to most problems
Enjoy, Have FUN! H.Merijn
Deepak Extross
Honored Contributor

Re: Search Pattern question

Attached is a small executable called ngrep to do just this. It's in C, so you cant modify it - but you can use a wrapper script to customise the output to your needs.
The usage is as follows:
ngrep -n 1 -p Dest -f
where
-n 1 : get the matching line and 1 line before it
-p Dest : search pattern is "Dest"
-f : filename in which to search.

Deepak Extross
Honored Contributor

Re: Search Pattern question

Attaching again - this time in Winzip format.
Seems like there was a problem with the last attachment.
H.Merijn Brand (procura
Honored Contributor

Re: Search Pattern question

That's all standard in GNU grep, even colourizing the matched pattern:

Context control:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
-NUM same as --context=NUM
--color[=WHEN],
--colour[=WHEN] use markers to distinguish the matching string
WHEN may be `always', `never' or `auto'.

Enjoy, Have FUN! H.Merijn
Vasudevan MV
Frequent Advisor

Re: Search Pattern question

Deepak,

Thanks for ur response, but the executable is not running on my box. I think some compatibility problem

ERROR:
/usr/lib/dld.sl: Can't open shared library: /usr/lib/libc.2
/usr/lib/dld.sl: No such file or directory
Abort(coredump)

Vasu
H.Merijn Brand (procura
Honored Contributor

Re: Search Pattern question

Ceesjan van Hattum
Esteemed Contributor

Re: Search Pattern question

Simple and effective onliner:

awk '{if ($0!="Dest"){ prev=$0 } else printf "%s,%s\n",prev,$0; }' inputfile

It gives:
15,Dest
66,Dest

Regards,
Ceesjan