I would like to do the following in bash shell.
file a
a:1
b:2
c:3
file b
a:work:apple
b:baby:banana
c:candy:cat
d:desk:dog
I would like to match field 1 in file a to file b, if there's a match I would like
to append field 2 in file a to field 3 in file b.
Thank you.
Try:
join -t: -j 1 -o 2.1,2.2,2.3,1.2 fileA fileB
thank you for your quick post. I was wondering if this can be done with awk ?
actually, I want the output appear like this:
a:work:apple1
b:baby:banana2
c:candy:cat3
Thanks
awk 'FNR==NR { a[$1]=$2;next} $1 in a {print $0a[$1] }' FS=":" file1 file2
thank you much, this exactly what I need.
I have same problem with phamp008 .
So if file a :
work
baby
candy
file b
a:work:apple
b:baby:banana
c:candy:cat
d:desk:dog
How can i do to ouput :
a:work:apple
b:baby:banana
c:candy:cat
Thank U!
nawk -F: 'FNR==NR {a[$1]++; next} $2 in a' fileA fileB
Or:
nawk -F: 'FNR==NR{a[$1];next}$2 in a' fileA fileB
Or even:
grep -f <(IFS='
';printf "^[^:]*:%s:\n" $(<fileA)) fileB