1832647 Members
2839 Online
110043 Solutions
New Discussion

Re: Scripting challenge

 
SOLVED
Go to solution
Matthew Pegge_1
Frequent Advisor

Scripting challenge

We have a number of jobs that run on 200+ systems fetching info back into a file. This looks something like:

gb656_1
/usr4/SDF
/usr4/LXD
/usr4/DFG

gb650_1
/usr4/FGH
/usr4/OIU

What I want to do is grep for a specific /usr4/??? line in the file. This is fine but is there anyway that same grep command could display the line plus a certain number of lines above/below the line I am grepping for? Basically as well as telling me the line exists I would also like to display the server (gb656_1) it is associated with.

Any ideas?
9 REPLIES 9
Mark Grant
Honored Contributor

Re: Scripting challenge

If you can get hold of GNU grep (it might be the same as on HPUX but I'm a bit HPUX challenged today so can't confirm) you can use the -A and -B which show you a few line of context before and after the match as in

grep -B 3 LXD in your case.
Never preceed any demonstration with anything more predictive than "watch this"
H.Merijn Brand (procura
Honored Contributor

Re: Scripting challenge

# perl -ne'BEGIN{$p=shift}m{^([^/]+)} and$d=$1;/$p/ and print "$d:$_"' pattern file

You can also use the extended features of GNU grep, which is MUCH better than the grep you get from HP

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
john korterman
Honored Contributor
Solution

Re: Scripting challenge

Hi,
try running the attached script with 3 params:
$1=the string to search for
$2=the number of lines to print before and after $1
$3=inputfile

regards,
John K.
it would be nice if you always got a second chance
Matthew Pegge_1
Frequent Advisor

Re: Scripting challenge

The GNU grep you talked about? Where do you get it from does it stop the HPUX grep from functioning? etc..
H.Merijn Brand (procura
Honored Contributor

Re: Scripting challenge

GNU grep available from my site:

Singapore https://www.beepz.com/personal/merijn/
Rotterdam http://www.cmve.net/~merijn/
Seattle http://ww.hpux.ws/merijn/

or from http://hpux.connect.org.uk/hppd/hpux/Gnu/grep-2.5.1/

in order to have it overrule HP's grep, just e sure to have your $PATH changed so that it finds GNU grep before HP's grep

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Mark Grant
Honored Contributor

Re: Scripting challenge

You can get it here and it is a drop in replacement for HPUX grep. It is completely compatible with HPUX grep only it's a lot better. Existing scripts that grep will not notice the difference. However, if you were worried, you could always copy your original grep somewhere else and just use a different path to it.

http://hpux.connect.org.uk/hppd/hpux/Gnu/grep-2.5.1/
Never preceed any demonstration with anything more predictive than "watch this"
Matthew Pegge_1
Frequent Advisor

Re: Scripting challenge

Thanks Guys! That just about sorts it out both short and long term!! You're all great!
Graham Cameron_1
Honored Contributor

Re: Scripting challenge

If you don't want to (or can't) get GNU grep, it can be done with awk.
The tricky bit is the lines before the pattern, which have to be kept in a buffer.
To show 10 lines before, 8 after, use something like:


awk -v before=10 -v after=8 '
{ibuf[NR]=$0}
/\/user4\/xxx/ {for (i=NR-before;isplit ("", ibuf) # Clear the array
counter=after+1}
(counter>0) {counter--;print}
' YOURFILE

This isn't terribly efficient as it saves every line in a buffer, although it does clear the buffer out each time it encounters the search string.
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Hein van den Heuvel
Honored Contributor

Re: Scripting challenge


Matthew,

Sounds like all you had to do is teach awk (or perl) to remember the lastserver node seen and report that upon match:

awk '/^[^\/]/{node=$0} /FGH/{print node " : " $0}' x
gb650_1 : /usr4/FGH

Graham wrote:
" The tricky bit is the lines before the pattern, which have to be kept in a buffer."
:
" This isn't terribly efficient as it saves every line in a buffer"

Using the modulus operator "%" it is trivial to create a small 'circular' buffer. See below.

", although it does clear the buffer out each time it encounters the search string."

That might not be desritable in case you can have two matches within the selected window size.

Here is a one-line 'circular buffer" example with windows size in variable W (4):

awk 'BEGIN {W=4} {line[i++%W]=$0} /FGH/{for (j=i-W;j
hth someone,
Hein.