How can we do this?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can we do this?
# 1  
Old 08-28-2008
MySQL How can we do this?

Hi,

I am new to shell scripting. I want to execute a task here.
In a file test.sh, there occurs a string test. I am now writing a script in another file ( let it be example.sh).

How can i print the next line from test.sh from the occurrence of the string xxx.

In detail, I want to print the next line to the line in which a string xxx is there in the file test.sh.

Thanks,
Prakash.Smilie
# 2  
Old 08-28-2008
Code:
awk '/xxx/{getline;print;exit}' file

Regards
# 3  
Old 08-28-2008
sed -n '/xxx/{n;p;}' Filename
# 4  
Old 08-29-2008
Hi,

Thanks for your replies. I tried Franklin's reply. Its working but there is one problem with it.

In my file the string xxx will occur many times. But I want to print the line next to the string xxx which is in the line with another string yyy

In detail, I will check for string yyy. If the line which has yyy also contains a string xxx, I want to print the next line to that line .

Please help me in doing that.

I did not try to execute manosubsulo's reply as I did not get him correctly what actually he mean with his n and p.

Thanks,
Prakash.
# 5  
Old 08-29-2008
Code:
awk '/yyy/ && /xxx/{getline;print}' file

In sed, 'n' means read the next line into the pattern space, 'p' means print the pattern space.
# 6  
Old 08-29-2008
sed -n '/xxx/{n;p;}' Filename

You can also use regular expression in the search string place xxx.
# 7  
Old 08-29-2008
Hi, Thanks for your reply but its not working

i think the error may be in writing yyy there.
Actually this yyy is the word which i am searching for. I am giving yyy as the command line to get the output.

So in your reply i am using

awk '/$1/ && /xxx/{getline;print}' file

I have also tried with

awk '/grep $1/ && /xxx/{getline;print}' file

but nothing worked.

Actually the line i am using to print is as below

cat abc.sh| grep $1 | grep Failure | awk '/grep $1/ && /Failure/{getline;print;exit}' /root/shell/abc.sh

Here abc.sh is the file on which i am doing this.

Here $1 is the word yyy as mentioned above.

This statement is another file in the same location /root/shell/

Thanks,
Prakash.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question