Sum values & compare with footer


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum values & compare with footer
# 1  
Old 10-21-2008
Sum values & compare with footer

Hi All,

I have a file abc.txt with 3 fileds. Field 3 contains amount. Also at the end of file there is a Footer record, which contains total amount.

I need to calculate total sum of these fields & need to compare it with footer record. I have serched in thi site, many has asked this questions, but i am not able to find out solution.

File
===
AM8906~JHUT56~100
AM5640~RESWD~200

Checksum~ 300

I need to get sum (100+200) & then to compare it with Checksum. Can someone help me on this
# 2  
Old 10-21-2008
awk -F'~' '{ if($1!="Checksum") s+=$3
else if(s==$2) print "Sum Matched with Footer"
else print "Sum is not Matching with Footer"
}' abc.txt
# 3  
Old 10-21-2008
Quote:
Originally Posted by Amit.Sagpariya
I have serched in thi site, many has asked this questions, but i am not able to find out solution.
if you are finding an EXACT solution to your problem, then i can say you will not find it. However similar ones, you can.

Code:
awk -F"~" '!/Checksum/{total+=$NF}/Checksum/{ print total==$NF ? "ok" :"not ok"}' file

# 4  
Old 10-21-2008
Code:
line=`cat filename | wc -l`
nawk -v l="$line" -F"~" '{
if(NR<l)
	p=p+$3
else
	c=$2
}
END{
	if(p>c)
		print "Sum of parts is larger than the checksum"
	else if (p<c)
		print "Sum of parts is less than the checksum"
	else
		print "Match"
}' filename

# 5  
Old 10-22-2008
Hi all,

there is a little bit changes... i am cutting the amount field from the source file and putting into one file.... called amount_1

amount_1
========
00000000000000006247.5
0000000000000000001071
0000000000000000003590
00000000000000001229.5
0000000000000000000400
0000000000000000785.77
0000000000000000012472
0000000000000000374.98
0000000000000000001550

what i need is to get the sum of this number & store it into a variable.

Then i will cut the total_amount from source file & store it in a variable. Then i will compare these 2 variable to decide future actions...

i have tried to do R&D from above 2 diff codes, but not able to do so... can you please help me
# 6  
Old 10-22-2008
Hi All,

I have worked on almost all solution got from google. but none of this is working.

1
sum=0

for i in 1 2 3 4 5 6
do
echo $i
((sum=sum+i))
done
echo "Summed up value is $sum"

2.
for i in `list whatever column you want`
do
sum=$(echo "$sum + $i"|bc)
done
echo "Summed up value is $sum"


can someone please help me
# 7  
Old 10-22-2008
Quote:
Originally Posted by Amit.Sagpariya
amount_1
========
00000000000000006247.5
0000000000000000001071
0000000000000000003590
00000000000000001229.5
0000000000000000000400
0000000000000000785.77
0000000000000000012472
0000000000000000374.98
0000000000000000001550
Code:
# eval $(awk '{a+=$0}END{printf "%s=%.5f\n","var", a}' amount_1)
# echo $var
27720.75000

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk sum of 2 arrays and compare

i'm new to awk, and i've been searching on the forum for sum of a column but all the scripts does sum a column of an entire file. I've a file like this: cat file.txt 1234 5678 5678 1234 I want to use awk to do sum of each column per line not entire file, compare the two then write the... (1 Reply)
Discussion started by: chofred
1 Replies

2. UNIX for Dummies Questions & Answers

sum values based on ID

Hi, I would like to be able to sum up the counts of a column by the ID of another column. Example (although the actual file I have has thousands of IDs): Input file: A1BG-AS1:001 3 A1BG-AS1:002 0 A1BG-AS1:003 2 A1CF:001 1038 A1CF:002 105 A1CF:003 115 A1CF:004 137 Desired output... (3 Replies)
Discussion started by: fadista
3 Replies

3. Shell Programming and Scripting

LINUX - How to compare the values in 2 files & exit from the script

Hi All, I have a requirement where I need to compare 2 files & if the values in the files match, it should proceed, else exit the script without proceeding further. For e.g : Scenario 1 In this case, the script should exit without proceeding further. Scenario 2 In this case, the script... (7 Replies)
Discussion started by: dsfreddie
7 Replies

4. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

5. Shell Programming and Scripting

Add header and footer with record count in footer

This is my file(Target.txt) name|age|locaction abc|23|del xyz|24|mum jkl|25|kol The file should be like this 1|03252012 1|name|age|location 2|abc|23|del 2|xyz|24|mum 2|jkl|25|kol 2|kkk|26|hyd 3|4 Column 1 is row indicator for row 1 and 2, column indicator is 1,for data rows... (1 Reply)
Discussion started by: itsranjan
1 Replies

6. Shell Programming and Scripting

Copying the Header & footer Information to the Outfile.

Hi I am writing a perl script which checks for the specific column values from a file and writes to the OUT file. So the feed file has a header information and footer information. I header information isaround107 lines i.e. Starts with START-OF-FILE ....... so on .... ... (11 Replies)
Discussion started by: filter
11 Replies

7. Shell Programming and Scripting

How to sum up two decimal values?

I am running the following script : cat ind_sls_extr_UX.out_sorted | while read each_rec do count=`echo "${each_rec}" | cut -c1-2` if then final_amount=0 amount=`echo "${each_rec}" | cut -c280-287` echo "${amount}" final_amount=`expr ${amount} + ${amount}` ... (7 Replies)
Discussion started by: mady135
7 Replies

8. UNIX for Dummies Questions & Answers

How to compare 2 files & get specific value & replace it in other file.

Hiiii Friends I have 2 files with huge data. I want to compare this 2 files & if they hav same set of vales in specific rows & columns i need to get that value from one file & replace it in other. For example: I have few set data of both files here: a.dat: PDE-W 2009 12 16 5 29 11.11 ... (10 Replies)
Discussion started by: reva
10 Replies

9. UNIX for Dummies Questions & Answers

Aggregate values in a file & compare with sql output

Hi, I have a file containing the following data: junk123junk723itemcode001qty01price10total10junkjunk junk123junk723itemcode002qty02price10total20junkjunk .. .. .. could be 5000+ lines I have an algo and need a code to implement this: 1. Linecount = wc -l (should give 5000) 2. For i... (1 Reply)
Discussion started by: shiroh_1982
1 Replies

10. Shell Programming and Scripting

rowcnt except Header & Footer

Hi Gurus, My requirement is, I am passing a file1.dat into this(rowcnt.sh) script,but returning a wrong value of -2.(it should be 4).Becoz my file1.dat contains 6records incl: Header & Footer.(6-2=4) wrong output: ------- #sh rowcnt.sh file1.dat -2 actual_cnt except HDR & FTR should be:... (3 Replies)
Discussion started by: vsubbu1000
3 Replies
Login or Register to Ask a Question