Date Name 352020252365988 Reference
Date2 Name2 546876543215667 Reference
I want to search for a match of Field 1 of file A in file 2. If there is a match print the line that contains the match, and append field 2 of File A at the end of each matched line.
#!/bin/sh
while read line
do
lookup=`echo "$line" | awk -vFS=',' '{print $1}'`
field=`echo "$line" | awk -vFS=',' '{print $2}' | sed 's/^ //'`
match=`grep "\<$lookup\>" file_b`
if [ "$?" -eq "0" ]; then
echo "$match" | while read record
do
echo "$record $field"
done
fi
done < file_a
assuming your files are named file_a and file_b.
The nested while is in case you have duplicate records and more than one record matches.
You'll need to redirect the output of this script to a file obviously.
#!/bin/sh
while read line
do
lookup=`echo "$line" | awk -vFS=',' '{print $1}'`
field=`echo "$line" | awk -vFS=',' '{print $2}' | sed 's/^ //'`
match=`grep "\<$lookup\>" ${2}`
if [ "$?" -eq "0" ]; then
echo "$match" | while read record
do
echo "$record $field"
done
fi
done < ${1}
You can call this with ./my_script file_a file_b - i.e. specifying the files at the prompt as you wish!