The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grepping columns ScKaSx UNIX for Dummies Questions & Answers 8 02-29-2008 06:44 PM
Grepping issue.. LinuxRacr Shell Programming and Scripting 9 02-28-2008 12:26 PM
grepping around cbeauty Shell Programming and Scripting 9 08-29-2007 02:56 AM
Grepping for strings t4st33@mac.com UNIX for Dummies Questions & Answers 5 06-21-2007 12:51 AM
grepping for a sentence eloquent99 UNIX for Dummies Questions & Answers 2 04-23-2003 03:40 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-21-2003
eloquent99 eloquent99 is offline
Registered User
  
 

Join Date: Jun 2002
Posts: 37
grepping

Is there a way to grep for something and then print out 10 lines after it.
for example if I want to grep for a word, then output the following 10 or whatever number of lines after the word.
  #2 (permalink)  
Old 04-21-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Maybe this...
sed -n '/word/{N;N;N;N;N;N;N;N;N;p;}'
  #3 (permalink)  
Old 04-22-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
I created a different script you can use. Although Pederabo's is waaay nicer looking and faster, you can use this if you wanted to print out more lines than just 10 (like, say 100)..

Replace the 4 on line 8 with the number of lines you want to print after the search string is found:

Code:
 1  for l in `sed -n '/someString/{=;}/g' file`
 2  do
 3  a=1;b=0
 4    while read LINE
 5    do
 6      if [ $a -eq `expr $l + 1` ]
 7      then
 8        b=4
 9      fi
10      if [ $b -gt 0 ]
11      then
12        echo $LINE
13      fi
14      b=`expr $b - 1`
15      a=`expr $a + 1`
16    done < file
17  done


Last edited by oombera; 04-22-2003 at 11:34 AM..
  #4 (permalink)  
Old 04-22-2003
plelie2 plelie2 is offline
Registered User
  
 

Join Date: Jul 2002
Location: Belgium
Posts: 27
grepping

The above is a possibility. However you'll only get the occurences where there are exactly the number of requested lines. If the requested string would be placed at the end of the file you would not see it.

I should do the trick with a little script

Code:
lnnr=10;
num_of_lines=10;
while read line
do
  if (( $lnnr < $num_of_lines ))
  then
    echo $line
    (( lnnr=$lnnr + 1 ))
  else
    echo $line | grep "string or whatever" >/dev/null 2>&1
    if (( $?==0 ))
    then 
      lnnr=0
      echo $line
    fi
  fi
done < inputfile



I added code tags for readability -- Perderabo

Last edited by Perderabo; 04-22-2003 at 11:17 AM..
  #5 (permalink)  
Old 04-22-2003
pbonilla pbonilla is offline
Registered User
  
 

Join Date: Aug 2002
Location: arizona
Posts: 11
why not just use head or tail. grep "word" file/location | tail -10 or head -10
  #6 (permalink)  
Old 04-22-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
tail -10 and head -10 print out the first 10 lines and last 10 lines, respectively, of a file. They cannot be used to pull lines from the middle of a file. Plus what you've done will output one or more lines containing the word "word" and then try to feed those lines into the tail or head command, which actually needs a filename.
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 02:33 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