If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > searching the line + next 20 lines

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-28-06, 15:13
varun_751980 varun_751980 is offline
Registered User
 
Join Date: Nov 2003
Location: India
Posts: 114
Lightbulb searching the line + next 20 lines

Hello Friends

I want to search a pattern in a simple file in UNIX. I can use the grep command for this but I want to get the search-pattern line and the next 20 lines redirected in some other file. Is there any option in grep or some other way through which I can get the output.
Reply With Quote
  #2 (permalink)  
Old 01-29-06, 11:24
hyperbole hyperbole is offline
Registered User
 
Join Date: Jan 2006
Posts: 32
You can do this with sed or awk. I have always preferred awk to sed, but I think it might be easier to do this one in sed.

Code:
   sed -n -e '/pattern/,+20p' file
should do what you want.



.
Reply With Quote
  #3 (permalink)  
Old 01-30-06, 07:34
varun_751980 varun_751980 is offline
Registered User
 
Join Date: Nov 2003
Location: India
Posts: 114
Unhappy Not working

:-)

bsfbl85:/oq/logs/batch/dl_hist>sed -n -e '/BA7400A/ ,+20p' uoq1002.scriptlog.20060129
Unrecognized command: /BA7400A/ ,+20p
bsfbl85:/oq/logs/batch/dl_hist>sed -n -e '/BA7400A/,+20p' uoq1002.scriptlog.20060129
sed: command garbled: /BA7400A/,+20p


Sorry the command is not working. Am i doing something wrong or there is some alternate way to achieve this.
Reply With Quote
  #4 (permalink)  
Old 01-30-06, 13:41
hyperbole hyperbole is offline
Registered User
 
Join Date: Jan 2006
Posts: 32
You must be using a different version of sed than I am. The documentation says that the +n syntax for the second address is a GNU extension. I ran a test of the above command with sed on Fedora Core 3 and it works fine.

I guess the question is what version of UNIX are you running and can you install GNU's version of sed on it. Otherwise I guess you'll need to use awk to do this.



.
Reply With Quote
  #5 (permalink)  
Old 01-30-06, 19:09
varun_751980 varun_751980 is offline
Registered User
 
Join Date: Nov 2003
Location: India
Posts: 114
Exclamation urgent

Quote:
Originally Posted by hyperbole
You must be using a different version of sed than I am. The documentation says that the +n syntax for the second address is a GNU extension. I ran a test of the above command with sed on Fedora Core 3 and it works fine.

I guess the question is what version of UNIX are you running and can you install GNU's version of sed on it. Otherwise I guess you'll need to use awk to do this.



.

Can anybody help with awk or some shell script to achieve the task. Friends this is urgent
Reply With Quote
  #6 (permalink)  
Old 01-30-06, 19:42
hyperbole hyperbole is offline
Registered User
 
Join Date: Jan 2006
Posts: 32
Place the following code into a file called script.awk:
Code:
BEGIN         { count = 21 }
/pattern/     { count = 0; }
count <= 20 { print; count++; }
from the command line run:
awk -f script.awk file-you-want-to-search



.
Reply With Quote
  #7 (permalink)  
Old 02-02-06, 19:51
varun_751980 varun_751980 is offline
Registered User
 
Join Date: Nov 2003
Location: India
Posts: 114
Thanks

Thanks a lot hyperbole , this worked.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On