Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Input file1 is:

dog 123 4335
cat 13123 23424 
deer 2131 213132
bear 2313 21313

I give the match the pattern from in other file ( like dog 123 4335 from file2).

I match the pattern of the line is dog 123 4335 and after printing all lines without match line my output is:

cat 13123 23424
deer 2131 213132
bear 2313 21313

If only use without address of line only use the pattern, for example 1s how to match and print the lines?

share|improve this question
add comment

5 Answers

Assuming you want to match the whole line with your pattern, this works:

sed -n '/^dog 123 4335$/ { s///; :a; n; p; ba; }' 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.
  • s/// empties pattern space, assuming you do not want the pattern included in the output.
  • :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 fil2, 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 is passed on to tail (+1).

Note that not all tails support the plus notation.

share|improve this answer
add comment

One way using awk:

awk 'NR==FNR{a[$0];next}f;($0 in a){f=1}'  file2 file1

where file2 contains your search patterns. First, all the contents of file2 are stored in the array "a". When the file1 is processed, every line is checked against the array, and printed only if is not present.

share|improve this answer
    
I think the OP wants to output every line following the pattern. –  Thor Nov 23 '12 at 11:41
    
@Thor : thanks for pointing it out, updated it now... –  Guru Nov 23 '12 at 11:57
    
Nicely done :). –  Thor Nov 23 '12 at 13:04
add comment

$ more +/"dog 123 4335" file1

share|improve this answer
add comment

Since awk isn't expressly disallowed, here's my offering assuming 'cat' is the match.

awk '$0 ~ /cat/ { vart = NR }{ arr[NR]=$0 } END { for (i = vart; i<=NR ; i++) print arr[i]  }' animals.txt
share|improve this answer
add comment
sed -e '1,/dog 123 4335/d' file1

If you need to read the pattern from a file, substitute it into the sed command. If the file contains a sed pattern:

sed -e "1,/$(cat file2)/d" file1

If the file contains a literal string to look for, quote all special characters. I assume the file contains a single line.

sed -e "1,/$(sed 's/[][\\\/^$.*]/\\&/g' file2)/d" file1

If you want the match to be the whole line, not just a substring, wrap the pattern in ^…$.

sed -e "1,/^$(sed 's/[][\\\/^$.*]/\\&/g' file2)\$/d" file1
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.