Reputation: 35
I have a File_1.txt with below data in it.
1234
2567
The second file File_2.txt has the below data in it.
2015-10-30,1234,zxcfgdk,kyfz
2015-10-30,2567,askjfkj,aaaa
2015-10-30,8888,bsjfjah,iojk
I want to compare the File_1.txt data with column2 data of File_2.txt. If it matches then redirect the entire row of File_2.txt to a seperate file. And the one's which are not matched to anothe file.
Have googled many pages. But was not of any help.
Thanks !
Upvotes: 1
Views: 52
Reputation: 10865
With awk
:
$ cat s.awk
BEGIN { FS="," }
NR==FNR { a[$1]; next }
$2 in a { print > "matched.txt" }
!($2 in a) { print > "missing.txt" }
$ awk -f s.awk f1 f2
$ cat missing.txt
2015-10-30,8888,bsjfjah,iojk
$ cat matched.txt
2015-10-30,1234,zxcfgdk,kyfz
2015-10-30,2567,askjfkj,aaaa
With grep
:
Another possibility is simply to use grep, but you need to be sure that the values in file1 will never appear (word delimited) in file2 outside of field2:
$ grep -w -f f1 f2
2015-10-30,1234,zxcfgdk,kyfz
2015-10-30,2567,askjfkj,aaaa
$ grep -v -w -f f1 f2
2015-10-30,8888,bsjfjah,iojk
Upvotes: 1