The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-28-2008
prakashreddy prakashreddy is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 17
Thumbs up 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.
  #2 (permalink)  
Old 08-28-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Code:
awk '/xxx/{getline;print;exit}' file
Regards
  #3 (permalink)  
Old 08-28-2008
manosubsulo manosubsulo is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 87
sed -n '/xxx/{n;p;}' Filename
  #4 (permalink)  
Old 08-29-2008
prakashreddy prakashreddy is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 17
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 (permalink)  
Old 08-29-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
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 (permalink)  
Old 08-29-2008
manosubsulo manosubsulo is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 87
sed -n '/xxx/{n;p;}' Filename

You can also use regular expression in the search string place xxx.
  #7 (permalink)  
Old 08-29-2008
prakashreddy prakashreddy is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 17
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0