Assuming you want to match the whole line with your pattern, with GNU sed
, this works:
sed -n '/^dog 123 4335$/ { :a; n; p; ba; }' infile
Standard equivalent:
sed -ne '/^dog 123 4335$/{:a' -e 'n;p;ba' -e '}' infile
With the following input (infile
):
cat 13123 23424
deer 2131 213132
bear 2313 21313
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
The output is:
cat 13123 23424
deer 2131 213132
bear 2313 21313
Explanation:
/^dog 123 4335$/
searches for the desired pattern.
:a; n; p; ba;
is a loop that fetches a new line from input (n
), prints it (p
), and branches back to label a :a; ...; ba;
.
Update
Here's an answer that comes closer to your needs, i.e. pattern in file2, grepping from file1:
tail -n +$(( 1 + $(grep -m1 -n -f file2 file1 | cut -d: -f1) )) file1
The embedded grep and cut find the first line containing a pattern from file2, this line number plus one is passed on to tail, the plus one is there to skip the line with the pattern.
If you want to start from the last match instead of the first match it would be:
tail -n +$(( 1 + $(grep -n -f file2 file1 | tail -n1 | cut -d: -f1) )) file1
Note that not all versions of tail support the plus-notation.