1830235 Members
2426 Online
109999 Solutions
New Discussion

Re: script

 
SOLVED
Go to solution
G.Kumar
Frequent Advisor

script

hai;


May be a simple question

I want a help to write one script.My requirement is search for a string and grep the next line to the line having the string(not the same line).

Any idea

thanks in advance
G.kumar
7 REPLIES 7
Sachin Patel
Honored Contributor

Re: script

Hi
In perl:

#!/usr/local/bin/perl

open(FILE,"filename") || die "can't open file\n";
$string_match=0;

while
{
if (string_match == 1){
print "$_ is your next line";
}
if (/string/) {
next;
$string_match=1;
}
else{
next;
}
}#end of while

Sachin
Is photography a hobby or another way to spend $
James R. Ferguson
Acclaimed Contributor

Re: script

Hi:

Try something like this:

# awk '/token1/ {getline;if (/token2/) {print $0}}' myfile

...JRF...
Sridhar Bhaskarla
Honored Contributor
Solution

Re: script

sed -n '/your_string/,$p' your_file |sed -n '2p'

This should work.

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
Paula J Frazer-Campbell
Honored Contributor

Re: script

Hi

If I understand you correctly then this should do it:-

Cat | grep | grep (and so on)

If the data is in a column format :-

dataA dataB DataC DataD

and you wish to just pick out the "dataC" column then

Cat | grep | awk '{print $3}'

HTH

Paula


If you can spell SysAdmin then you is one - anon
Jay Newman
Frequent Advisor

Re: script

Hi.
Let me make sure I understand your question.
Do you want to search to see what files contain a given string, then read the line after the line that contains this string, grepping for a second string?

If not, could you please clarify.
It also may become more complicated if you are recursively searching a directory or file system, since you would need to issue a find command to generate a list of files, then use loop to do a grep against each item in the list.
"Success is defined by getting up one more time than you fall down."
G.Kumar
Frequent Advisor

Re: script

hi

Thanks for the reply.
"sed" works.

one more question...

If my string is variable can i use sed ?

thanks in advance
Sridhar Bhaskarla
Honored Contributor

Re: script

Hi Kumar,

Yes you can...

sed -n '/'$YOUR_VARIABLE'/,$p' your_file |sed -n '2p'

Please note those single quotes on $YOUR_VARIABLE.

-Sri

You may be disappointed if you fail, but you are doomed if you don't try