Skip to main content
2 of 13
Corrects answer.
kenorb
  • 22.1k
  • 18
  • 149
  • 172

Normally grep with -f parameter will print lines with at least one pattern, e.g.

grep -f args.txt file.txt

In your case it won't work.

So to print lines which matches all patterns, you can try this command:

while read n text; do [ $n -eq $(wc -l < args.txt) ] && echo $text; done < <(while read patt; do grep $patt text.txt; done < args.txt | sort | uniq -c)

Explanation:

  1. The inner while loop will print all lines which matches at least one pattern in text.txt using pattern list from args.txt file.
  2. Then this list is sorted (sort) and counted for number of occurrences (uniq -c).
  3. The outer while loop will print only lines which have the same number of occurrences that number of patterns in args.txt (which is 3).
kenorb
  • 22.1k
  • 18
  • 149
  • 172