Operating System - HP-UX
1833873 Members
1786 Online
110063 Solutions
New Discussion

Re: scripting help needed

 
SOLVED
Go to solution

scripting help needed

Hi all,
I want to search for a string in an ascii file and the output should include the fifth and the eighth line below the match.

tia
Clemens
always look on the bright side of life
8 REPLIES 8
Justo Exposito
Esteemed Contributor

Re: scripting help needed

Hi Clemens,

Can you put a detailed example that you need?

Regards,

Justo.
Help is a Beatiful word
Deepak Extross
Honored Contributor

Re: scripting help needed

Hi,

You can try using awk and its getline function.
Just to give you an idea, lets say you have a file containing:
111
222
333
444
555
666
777
888
999
000

Try this:
cat aaa | awk '/111/ {getline; getline; getline; getline; getline x; getline; getline; getline y; print x; print y}'

The output of this is:
666
999

You may want to build on this to customise it to your specific requirement.
Carlos Fernandez Riera
Honored Contributor

Re: scripting help needed

Re: scripting help needed

Hi Deepak,
thanks for your quick answer. It matches my needs nearly complete.
Now I need to put this awk command in a loop to change the search string several times.
So I have to exchange
cat aaa | awk '/111/ ... against
cat aaa | awk '/$xxx/ ...
But how do I have to quote this variable?

Thanks again
Clemens
always look on the bright side of life
Ruediger Noack
Valued Contributor

Re: scripting help needed

Hi Clemens,

don't know the syntax for quoting of vars in regular expressions.
But you can try this:
cat aaa | awk '{if ($1 ~ YOUR_VAR) {getline; getline; getline; getline; getline x; getline; getline; getline y; print x; print y }}' YOUR_VAR=$PARAM
or change ($1 ~ YOUR_VAR)
with ($1 == YOUR_VAR)
if $PARAM is exactly the row in your input file.
YOUR_VAR=$PARAM is part of awk command.
Set your search string to PARAM.

Ruediger



Darrell Allen
Honored Contributor
Solution

Re: scripting help needed

Hi Clemens,

Consider the following contents of "file":
a
b bravo charlie junk
c charlie delta
d
e
f
g bravo 5th
h charlie 5th
this is it
1 bravo 8th
2 charlie 8th
3
4
5 this one
6
7
8 and this one
9
10
11

Try this script:
#!/usr/bin/sh
for var in "this is it" "bravo" "charlie delta"
do
awk ''/"$var"/' {getline; getline; getline; getline; getline x; getline; getline
; getline y; print x; print y}' file
done

Note the quoting of the arguments in the for loop. That enables the embedding of spaces in the search string.

The quoting of $var in the awk statement will preserve the spaces in the search and recognizes the difference between "this is it" (one space between the words) and "this is it" (two spaces between the words).

Here's another thing I found in testing. It may may not apply to your case but just in case it does...

Note that this awk will find multiple references of the search string in the file as long as they are not in the same 8 line block. For example, search for "charlie" in "file" will return the 5th and 8th lines after the line "b bravo charlie junk", will not return the lines following "c charlie delta", but will return the 5th and 8th following "2 charlie 8th". That's because the awk statement finds a match, checks the next 8 lines, then begins looking for another match.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Ceesjan van Hattum
Esteemed Contributor

Re: scripting help needed

Found it..:

awk -v arg=aaa -v pos1=5 -v pos2=9 '{
if (count==pos1) print $0
if (count==pos2) print $0
if (count>=1) count++
for (i=1;i<=NR;i++) {
if ($i==arg) count=1
}
} '

From the inputfile:
abc
aaa
abc1
abc2
abc3
abc4
abc5
abc6
abc7
abc8
abc9
abc10
abc11

It will give you the result:
abc5
abc9
(it starts counting after retrieval of searchstring "aaa".

Regards,
Ceesjan


Re: scripting help needed

Thanks Ruediger,
your version did what I want it to do.
Thanks Darrell,
you teached me the quoting I couldn't work out by myself!

See you
Clemens
always look on the bright side of life