LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-22-2010, 05:49 PM   #1
buee
Member
 
Registered: May 2009
Posts: 81

Rep: Reputation: 16
Adding lines of a file together


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
Code:
0 0
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"?
 
Old 02-22-2010, 07:39 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
I am too lazy to replay your script, but run it with sh -x yourscript and you can trace every program line and variable value. Since you designed the program yourself it is easy for you to understand where you expect what value.

Why do you put values in in.txt and out.txt. There is no need to do that, a variable once given a value retains this value in the entire script. If you need that value in a file, you can write it into that file at the very end of the program.

jlinkels
 
Old 02-22-2010, 08:17 PM   #3
buee
Member
 
Registered: May 2009
Posts: 81

Original Poster
Rep: Reputation: 16
Handy Info

Quote:
Originally Posted by jlinkels View Post
I am too lazy to replay your script, but run it with sh -x yourscript and you can trace every program line and variable value. Since you designed the program yourself it is easy for you to understand where you expect what value.

Why do you put values in in.txt and out.txt. There is no need to do that, a variable once given a value retains this value in the entire script. If you need that value in a file, you can write it into that file at the very end of the program.

jlinkels
Thanks for the sh -x tip, that will come in handy.

The reason I have it output like that is because the initial files are worthless after the script has run, I just need the totals, not the original output. Also, I will be rsyncing these to a different machine so they can be used for graphing purposes.
 
  


Reply

Tags
bash, line, math


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete Duplicate Lines in a file, leaving only the unique lines left xmrkite Linux - Software 6 01-14-2010 06:18 PM
replace several lines in a file with other lines in another file if condition yara Linux - General 12 10-27-2009 03:46 PM
Adding lines of text to beginning of a text file BillKat Programming 2 01-19-2009 10:40 AM
Substitute specific lines with lines from another file rahmathullakm Programming 4 01-10-2009 05:47 AM
adding text to lines in a file tpreitano Linux - General 2 10-04-2005 09:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration