Help required on grep command(Skip the first few lines from printing in the output)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help required on grep command(Skip the first few lines from printing in the output)
# 1  
Old 09-24-2009
Help required on grep command(Skip the first few lines from printing in the output)

Hi experts

I want the proper argument to the grep command so that I need to skip the first few lines(say first 10 lines) and print all the remaining instances of the grep output.
I tried to use grep -m 10 "search text" file*. But this gives the first 10 instances(lines) of the search string. But I want to skip the first 10 lines(instances) and print the remaining lines in the output.
I need to run this grep command on a group of files(100s) at once.
I hope my problem statement is clear.

Please help me in this regard.
Many thanks in advance.
# 2  
Old 09-24-2009
Code:
grep "search text" file | tail -n +11

# 3  
Old 09-24-2009
Problem with grep command..Very urgent

Quote:
Originally Posted by varontron
Code:
grep "search text" file | tail -n +11

Hi Varontron and all

It looks like you did not understood my question .
Suppose if I am grepping some text from three files, my output should be
file1:11th line onwards to last line from file1
file2:11th line onwards to last line from file2
file3:11th line onwards to last line from file3
I am not expecting the 11th line onwards in the cumulative file.

I hope problem statement is clear at this time.
Regards
Sidda
# 4  
Old 09-25-2009
so... if you want to grep a whole bunch of files with one command, you'll get one big set of output. You should get the filename at the front of each line in the output so you could then process the whole pig.

or...you could put the grep in a loop like this

Code:
for each in `ls dir_with_files`
do
  grep "searchtext" $each | tail -n +11 > dir_with_processed_files/$each
done

# 5  
Old 09-25-2009
Quote:
Originally Posted by varontron
so... if you want to grep a whole bunch of files with one command, you'll get one big set of output. You should get the filename at the front of each line in the output so you could then process the whole pig.

or...you could put the grep in a loop like this

Code:
for each in `ls dir_with_files`
do
  grep "searchtext" $each | tail -n +11 > dir_with_processed_files/$each
done

Thank you Varantron for your timely help.

I prefer the first method where one single command will get the required output from each with file identity.
For time being I am already using the second method of modifying the files and copying to other directory and then cat them together and process further. As I need work on roughly 2GB of input files the second method requires many file transfers/creating new directories etc. So I will be grateful if I can get single command to do this.
# 6  
Old 09-25-2009
well, even a single command is going to require some work, as I assume you'll need to output the results of each individual file to a separate file.

furthermore, your loop will need to be a little more complex because you'll have to iterate over the whole grep result and keep track of the file on the current line, the line number, etc.

I think it would look something like this...

Code:
cd mydir
grep "searchtext" * > results.txt
COUNT=1
OLD_FNAME=""
NEW_FNAME=""
while read line 
do
  NEW_FNAME=`echo $line|cut -f1 -d':'`
  if [ $OLD_FNAME = $NEW_FNAME ]
  then
    if [ $COUNT -gt 10 ]
    then
        # output the line, presumably without the filename
    fi 
    COUNT=$COUNT+1
  else
    OLD_FNAME = $NEW_FNAME
    COUNT=1
  fi
done < results.txt

# 7  
Old 09-25-2009
How about this:
Code:
sed '1,10d' file*

This User Gave Thanks to edidataguy For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search a text in file and retrieve required lines following it with UNIX command?

I have requirement to search for a text in the file and retrieve required lines that is user defined with unix command. Eg: Find the text UNIX in the below file and need to return Test 8 & Test 9 Test 1 Test 2 Test 3 Test 4 UNIX Test 5 Test 6 Test 7 Test 8 Test 9 Result can... (8 Replies)
Discussion started by: Arunkumarsak4
8 Replies

2. Shell Programming and Scripting

Printing the output of a gzip command intact

how can i get the printf command or the echo command to print the data that are inbetween the first and the last quotes? #!/bin/sh printf '%s\n' "^_<8b>^H^@U<8c>MX^@^Cí=ÙzÚH<97>×ð^Teìn<8c>Ób_<9d><9f>dXd<9b>^N^F7<82>8qâÎ'^K^Y^T<83>D<90>°M^Lý^Hó^Fs5w3ß|s5/ÐýbS%©<84>^DBH... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. SuSE

Find command doesn't pipe the output as required.

Hi, I am using below code snippet to echo/display the files found (matching a pattern from searchstring.out file) and the corresponding owner. while read j do echo "Pattern to search is:- $j" find / -name "*$j*" |\ while read k do echo "File found is:- $k" owner=$(ls... (9 Replies)
Discussion started by: Vipin Batra
9 Replies

4. AIX

Print output of grep command in multuple lines

HI All, I am using grep command to serach a pattern in a list of files and storing the output in a variable. Then i am applying some logic on that variable to get the required output. But Thing is that when the pattern is present mutiple times in a file, i am getting the output of grep in a... (3 Replies)
Discussion started by: goutam sahoo
3 Replies

5. Shell Programming and Scripting

Printing lines with non-redundant itens in the output

Hi all, I'm trying to select lines of a file and at the end do not have redundant itens: Input A_B K_A C_T A_O U_B P_C D_F Z_G W_U Output A_B C_T D_F Z_G W_U (2 Replies)
Discussion started by: valente
2 Replies

6. Shell Programming and Scripting

Need Help with grep printing multiple lines.

I need help in printing multiple lines using a grep command. The situation is like this. I have a file that contains large number of lines Now I need to find the the lines in the file such that if the word "AllServiceType" is found then the next line also gets printed. Does anyone... (6 Replies)
Discussion started by: m_usmanayub
6 Replies

7. Shell Programming and Scripting

How to filter only the last 'n' lines of a grep output?

I am running a grep query for searching a pattern, and the output is quite huge. I want only the last 200 lines to be displayed, and I am not sure if tail will do the trick (can tail read from std in/out instead of files?). Please help me out. (1 Reply)
Discussion started by: shell_newbie
1 Replies

8. UNIX for Dummies Questions & Answers

grep required pattern and next 2 or 3 lines

dear ones pl.kindly help me 1) how to print(grep) required pattern and following 2 or 3 lines. 2) grep required pattern(to print)+above 2 lines+below 2 or 3 lines.from a report file. ex: we have some report file kf askfjsk fksaj fk skf sjfksjd kff sjfkjs kf jskdjfklsd jfklsdf sdkfjsd fsd... (3 Replies)
Discussion started by: cvvsnm
3 Replies

9. Shell Programming and Scripting

GREP:Output all lines containing word

Output all lines in the file temp that contain the word dog using GREP only and in one line!!! I tried grep ']dog]' temp but it doesnt catch word dog when is at beginning or end, like: Our dog is nice /this OK Nice dog /this NOT dog good /this NOT Thank... (3 Replies)
Discussion started by: ljubayuu
3 Replies

10. Shell Programming and Scripting

Help required in grep command

Hi all, I need some help in grep command in a ksh script. Actually, i need to list all files having the file name like "BORD*.DAT" but exclude the files (from the list) having name like "BORD*mgt*.DAT". For that i used the following command: ls | grep "BORD*.DAT" | grep -v "BORD*mgt*.DAT" ... (4 Replies)
Discussion started by: panzer
4 Replies
Login or Register to Ask a Question