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.

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

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

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.

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...

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

How about this:

sed '1,10d' file*
1 Like

Hi.

I found the requirements unclear, and posting a few sample files and expected output would allow more on-point responses.

I assume you wish to search for matches in several files, but to omit the first few matches for each file -- that is, skip, say the first 11 matches for each file.

The utility cgrep can do this. Here is a script that reads 2 sample files containing mostly lines of the form "1.1, 1.2 .." for the first file, and "2.1, 2.2 ..." for the second file:

#!/usr/bin/env bash

# @(#) s1	Demonstrate cgrep skipping N matched lines.
# http://www.bell-labs.com/project/wwexptools/cgrep/

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) edges cgrep
set -o nounset

FILES="data*"
# Paste files, see edges:
echo
echo " Sample content of files, pasted, line numbers added:"
paste  $FILES |
expand -25 |
edges -l 3

echo
echo " Results:"
cgrep -h +N 11  '\.[0-9]' $FILES

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
edges (local) 307
cgrep - (local: ~/executable/cgrep May 29 10:59 )

 Sample content of files, pasted, line numbers added:
     1	first line of data1      first line of data2
     2	1.1                      2.1
     3	1.2                      2.2
   ...
    13	1.12                     2.12
    14	and so on, until         and so on, until
    15	the last line of data1   the last line of data2

 Results:
1.12
2.12

The cgrep utility is available at the AT&T site as noted. Compilation was straight-forward on both 32-bit and 64-bit systems.

If you don't prefer this approach, the solution suggested by varontron will work (after a few modifications).

Best wishes ... cheers, drl