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 at the same time, 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:
- The inner
whileloop will print all lines which matches at least one pattern intext.txtusing pattern list fromargs.txtfile. - Then this list is sorted (
sort) and counted for number of occurrences (uniq -c). - The outer
whileloop will print only lines which have the same number of occurrences that number of patterns inargs.txt(which is 3).
Another approach would be to remove all lines which does not match at least one pattern.
Here is the solution using Ex/Vim editor changing the file in-place:
while read patt; do ex +"v/$patt/d" -scwq text.txt; done < args.txt
Note: This will remove the unneeded lines from the file it-self.