I'm trying to output two certain fields of a very large text file to 2 very small text files. Then take those files and add all the lines together to come up with a total from each file (two totals).
Here's what I've got:
Code:
#!/bin/bash
echo "0" > /media/in.txt
echo "0" > /media/out.txt
cat /opt/zimbra/zmstat/mailboxd.csv | tail -60 | cut -f2 -d "," > /media/messages_in.txt
cat /opt/zimbra/zmstat/mailboxd.csv | tail -60 | cut -f5 -d "," > /media/messages_out.txt
while read $LINE; do
a=$(cat /media/messages_in.txt | head -1)
zero=$(cat /media/in.txt)
b=$(($zero+$a))
echo "$b" > /media/in.txt
awk 'FNR>1' /media/messages_in.txt
done < "/media/messages_in.txt"
while read $LINE; do
a=$(cat /media/messages_out.txt | head -1)
zero=$(cat /media/out.txt)
b=$(($zero+$a))
echo "$b" > /media/out.txt
awk 'FNR>1' /media/messages_in.txt
done < "/media/messages_out.txt"
in=$(cat /media/in.txt)
out=$(cat /media/out.txt)
echo "$in $out"
rm /media/*in.txt /media/*out.txt
exit 0
Breakdown:
Put 0 in a text file to be drawn by respective while loops for math later
Output last 60 integers to a file for total A (new integer every minute)
Output last 60 integers to a file for total B (new integer every minute)
The two while loops are supposed to be adding the lines together.
The echo commands at the end are for testing purposes, just to see the output.
However, when I run this, I get the output of
Which is obviously not what it's supposed to be.
Is there a more efficient way to do this or am I missing something in the script that would reset the values to "0"?