grepping


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grepping
# 1  
Old 04-21-2003
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  
Old 04-21-2003
Maybe this...
sed -n '/word/{N;N;N;N;N;N;N;N;N;p;}'
# 3  
Old 04-22-2003
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).. Smilie

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  
Old 04-22-2003
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

Smilie

I added code tags for readability -- Perderabo

Last edited by Perderabo; 04-22-2003 at 11:17 AM..
# 5  
Old 04-22-2003
why not just use head or tail. grep "word" file/location | tail -10 or head -10
# 6  
Old 04-22-2003
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grepping and replacing

Hey Friends, Need your help again. I have input.temp file as follows $cat input.temp Lakme|Beauty Products|Lipstick L'Oreal|Hair Care|Conditioner Lakme|Beauty Products|Lip gloss L'Oreal|Hair Care|Mild Shampoo Gala|Beauty Products|Mehndi Cones Lakme|Beauty Products|Eye Shadow... (2 Replies)
Discussion started by: anushree.a
2 Replies

2. Shell Programming and Scripting

Grepping more than one word

Dear Experts, Need your help. Typically we use "grep" to search and display a pattern in a txt file. However, here what we want is, we want to grep a line which contains 4 words any where in a line. For example. File has 10,000,000 lines in it out of which there is a particular line which... (1 Reply)
Discussion started by: anushree.a
1 Replies

3. UNIX for Dummies Questions & Answers

Grepping using -w and dashes (-)

I have a script to sort a list of arbitrary hosts and determine if they are supported by grepping them into a master supported list. I cut all the suffixes of the hosts in the arbitrary list, leaving the "short" hostname if you will, then grep -w them into the master list. For example: ... (1 Reply)
Discussion started by: MaindotC
1 Replies

4. Shell Programming and Scripting

grepping by digit

Hi all, Need your help here. I have a file with thousand of lines, as shown in example below KDKJAA 98324 OIDSAJ 324 KJAJAK 100 KJKAJK 89 JOIJOI 21 JDKDJL 12 UOIUOD 10 UDUYDS 8 UIUHKK 6 I would like to grep using... (5 Replies)
Discussion started by: masterpiece
5 Replies

5. UNIX for Dummies Questions & Answers

grepping between files

Hi I have two files File 1 alias HOME =.. alias DATA = ${DATA}/runtime1/test alias SQL = ${DATA}/find1dir/test alias SQL1 = ${HOME}/sql/orcl alias SQL2 =... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

6. Shell Programming and Scripting

Please help on grepping

Hi All, I have a log file and I want to parse the logfile with a script.A sample text is shown below: I would grep on "return code " on this file. Any idea how the lines above and below the grep patterns could also be extracted. Thanks! nua7 The runLoggingInstall return code is 0... (3 Replies)
Discussion started by: nua7
3 Replies

7. UNIX for Dummies Questions & Answers

grepping columns

Hi All, I was recently helped out 'big time' with my last post on changing multiple file formats (thx, scott1256ca and bakunin)! My new question is about selecting and displaying columns in a file using (possibly) grep. Several of my data files are spreadsheet format (columns separated by... (8 Replies)
Discussion started by: ScKaSx
8 Replies

8. Shell Programming and Scripting

Grepping issue..

I found another problem with my disk-adding script today. When looking for disks, I use grep. When I grep for the following disk sizes: 5242880 I also pick up these as well: 524288000 How do I specifically pick out one or the other, using grep, without resorting to the -v option? ... (9 Replies)
Discussion started by: LinuxRacr
9 Replies

9. Shell Programming and Scripting

grepping around

Using shell scripts, I use grep to find the word “error” in a log file: grep error this.log. How can I print or get the line 3 lines below the line that word “error” is located? Thanks in advance for your response. (9 Replies)
Discussion started by: cbeauty
9 Replies

10. UNIX for Dummies Questions & Answers

Grepping for strings

Hello. I have a dir of 1500+ dir. In these dirs is a file host, with a tag <x_tag>. I need to : 1. grep for all dir that contain this host file that contain <x_tag> 2. print a list of these host files containing <x_tag> is this better to egrep this? (5 Replies)
Discussion started by: t4st33@mac.com
5 Replies
Login or Register to Ask a Question