nawk 'NR==FNR{a[$1];next} {if($1 in a) print $1,"Found" else print}' OFS="," File_B File_A
The above code is not working help is appreciated
nawk 'NR==FNR{a[$1];next} {if($1 in a) print $1,"Found" else print}' OFS="," File_B File_A
The above code is not working help is appreciated
well this was easy, you are missing on the syntax
nawk 'NR==FNR{a[$1];next} {if($1 in a) print $1,"Found";print}' OFS="," File_B File_A
-----Post Update-----
sample output
bash-3.00$ nawk 'NR==FNR{a[$1];next} {if($1 in a) print $1,"Found";print}' OFS="," File_B File_A
NY,Found
NY
NJ,Found
NJ
PA
CA,Found
CA
VA,Found
VA
TN
This is not the output i am looking for. please see the required output
awk 'FNR==NR{a[$1]++;next}{if($1 in a)print $1,"Found"; else printf("%s%s\n", $1,OFS)}' OFS="," B A
-Devaraj Takhellambam
Or (use gawk, nawk or /usr/xpg4/bin/awk on Solaris)
awk 'NR == FNR { _[$1]; next }
($2 = $1 in _ ? "Found" : x) || 1
' OFS=, file_b file_a
if you have Python, an alternative
#!/usr/bin/env python
file2=[i.split()[0] for i in open("file2").read().split("\n")]
for line in open("file1"):
line=line.strip().split()
if line[0] in file2:
print line[0]," found"
else:
print line[0],","
output
# ./test.py
NY found
NJ found
PA ,
CA found
VA found
TN ,
And if you have perl, then:
perl -ne 'BEGIN{open(F,"fileb"); while(<F>){split;$found{$_[0]}="Found"} close(F)} {chomp; print "$_,$found{$_}\n"}' filea
Test:
$
$ cat filea
NY
NJ
PA
CA
VA
TN
$
$ cat fileb
NY hello
NJ 3
CA 1
VA 5
$
$ perl -ne 'BEGIN{open(F,"fileb"); while(<F>){split;$found{$_[0]}="Found"} close(F)} {chomp; print "$_,$found{$_}\n"}' filea
NY,Found
NJ,Found
PA,
CA,Found
VA,Found
TN,
$
tyler_durden