Reputation: 123
I would like to grep for aaa and bbb and ccc from file1.txt in any order on the same line on file2.txt
file1.txt
aaa bbb ccc
ddd fff ggg
hhh ddd jjj
jjj ccc
file2.txt
aaa bbb ccc ddd fff ggg --> output whole line since it matches with aaa bbb ccc of file1.txt
aaa ddd jjj hhh --> no output since it does not match with any search pattern in file1.txt
ccc ddd fff ggg jjj --> output whole line since it matches with jjj ccc of file1.txt
This is the script I came up so far. But does not output anything
gawk 'NR==FNR{a[$1,$2,2$3]++;next} ($0 in a)' file1.txt file2.txt
Upvotes: 1
Views: 156
Reputation: 67467
awk to the rescue! This is based on the example but not matching your description.
$ awk 'NR==FNR{a[$0];next}
{for(i in a) {
m=1; n=split(i,b," "); {
for(j=1;j<=n;j++)
if($0!~b[j]) m=0
}
}
} m{print}' f1 f2
will result
aaa bbb ccc ddd fff ggg --> output whole line since it matches with aaa bbb ccc of file1.txt
ccc ddd fff ggg jjj --> output whole line since it matches with jjj ccc of file1.txt
Note that line to line matching is not implemented as in your example.
Upvotes: 1
Reputation: 3394
This should work:
paste file1.txt file2.txt | awk -F '\t' '$1~/aaa|bbb|ccc/{ print $2}'
But as I mentionned in my comment there is a match on the 4th line of file1.txt but there is no 4th line in file2.txt so it won't print anything.
paste merge both file using a as the separator. it then feeds the output of paste to awk using as the separator and where $1
is the line of file1 and $2
is the line of file2.
Upvotes: 0