[TriLUG] bash script - join question
Chander Ganesan
chander at otg-nc.com
Tue Oct 24 14:43:44 EDT 2006
Shawn Hood wrote:
> I am trying to perform a join on two separate files. They are
> essentially look-up tables with a common key in the first field. It
> seems to only be matching the first row of one of the files. Any
> ideas?
>
> Contents of file1:
> INT9 00:11:25:fe:ab:c6
> INT3 00:14:5e:1d:bb:c2
> INT1 00:14:5e:1d:cb:0e
>
> Contents of file2:
> INT1 10.40.21.1
> INT2 10.40.21.2
> INT3 10.40.21.3
> INT4 10.40.21.4
> INT5 10.40.21.5
> INT6 10.40.21.6
> INT7 10.40.21.7
> INT8 10.40.21.8
> INT9 10.40.21.9
>
> Join syntax:
> join file1 file2
>
> Results:
> INT9 10.40.21.9 00:11:25:fe:ab:c6
>
> Desired results in no particular order:
> INT9 10.40.21.9 00:11:25:fe:ab:c6
> INT3 10.40.21.3 00:14:5e:1d:bb:c2
> INT1 10.40.21.1 00:14:5e:1d:cb:0e
>
Two ways come to mind:
- If both start with the word 'INT' followed by a number, strip the
word INT out and then use an array. You'll put file1 into an array, put
file2 into an array, and the match 'em up and print 'em out.
Failing that, I'd walk through the file using something like this:
FILE1="$1"
FILE2="$2"
IFS='
'
for RECORD in $(cat ${FILE1}); do
MATCH=$(grep ${FILE2} ^${RECORD%% *})
for LINE in ${MATCH}; do
echo ${MATCH} ${RECORD#* }
done
done
It's not all that efficient, but off the top of my head it's the most
straightforward way to do it. If each line has only one match you can
get away without the inner for loop...but as shown above it should
produce all permutations of combinations...even if entries with the same
"key" exist multiple times in both files..
Keep in mind that I didn't test this code...but it should work ;-)
Chander Ganesan
Open Technology Group, Inc.
One Copley Parkway, Suite 210
Morrisville, NC 27560
Phone: 877-258-8987/919-463-0999
>
>
More information about the TriLUG
mailing list