Reputation: 3576
I've two big files, a.txt and b.txt
each contains a lot of entries like this:
say a.txt has these:
12 entry1
23 entry2
34 entry4
...
b.txt has these:
14 entry1
25 entry3
14 entry4
...
how can I do the subtraction for the same entry, while leaving the uniq entries untouched? e.g.
a.txt - b.txt would give me
entry1: 12-14 = -2
entry2: 23
entry3: 25
entry4: 34-14 = 20
...
I couldn't figure it out, please help. I know awk should be the way to go, but just cannot figure it out.
Thanks
Upvotes: 0
Views: 1033
Reputation: 1254
This is a version without awk, it could help you while you finish your awk script:
#!/bin/bash
#substraction.sh
file1="a.txt"
file2="b.txt"
echo "">myTempFile.txt
fileTemp="myTempFile.txt"
lin1=$(wc -l $file1 | cut -d " " -f 1)
lin2=$(wc -l $file2 | cut -d " " -f 1)
if [ $lin1 -lt $lin2 ]; then
lineas=$lin2
myfileBIG=$file2
myfileTINY=$file1
else
lineas=$lin1
myfileBIG=$file1
myfileTINY=$file2
fi
ind=1
echo "Total of lines: "$lineas
#### myfileBIG with myfileTINY
while [ $ind -le $lineas ] #less than or equal to
do
mynum1=$(sed -n "$ind"p $myfileBIG | cut -d " " -f 1)
mydat1=$(sed -n "$ind"p $myfileBIG | cut -d " " -f 2)
mynum2=$(cat $myfileTINY | grep $mydat1 | cut -d " " -f 1)
#echo "$mynum1**$mydat1**$mynum2"
if [ -n "$mynum2" ]; then
result=$(($mynum1-$mynum2))
echo "$mydat1: $mynum1-$mynum2=$result"
echo "$mydat1: $mynum1-$mynum2=$result">>myTempFile.txt
else
result=""
echo "$mydat1: $mynum1"
echo "$mydat1: $mynum1">>myTempFile.txt
fi
ind=$(($ind+1))
#echo "Posicion: $ind de $lineas"
done
#### myfileTINY with myTempFile
echo "Using temp file..."
ind=1
if [ $lineas -eq $lin1 ]; then
lineas=$lin2
else
lineas=$lin1
fi
echo "Total of lines: "$lineas
while [ $ind -le $lineas ]
do
mynum1=$(sed -n "$ind"p $myfileTINY | cut -d " " -f 1)
mydat1=$(sed -n "$ind"p $myfileTINY | cut -d " " -f 2)
mynum2=$(cat $fileTemp | grep $mydat1 | cut -d " " -f 1)
#echo "$mynum1**$mydat1**$mynum2"
if [ -z "$mynum2" ]; then
echo "$mydat1: $mynum1"
echo "$mydat1: $mynum1">>myTempFile.txt
fi
ind=$(($ind+1))
#echo "Posicion: $ind de $lineas"
done
echo "...Done!"
echo "Result:"
sort -b myTempFile.txt
Execute follow this:
bash ./substraction.sh
Result:
Upvotes: 1