A summing issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A summing issue
# 1  
Old 05-05-2008
A summing issue

Hi All,

Here is the problem:

The input file is like as per below. Each record has 30 chars in total. Have to add the first 17 and the next 13 and append the output. Total records can vary.

000000004728800000000000003908
000000003005100000000000002484
000000002602200000000000002151
000000003418400000000000002825
000000001059900000000000000876
000000001146200000000000000947
000000000973700000000000000805
000000001561300000000000001290
000000001088600000000000000900
000000001146200000000000000947
000000001029300000000000000851
000000001034500000000000000855
000000000977700000000000000808
000000000888800000000000000735
000000000884200000000000000731
000000001061100000000000000877
000000002721600000000000002249
000000001040800000000000000860
000000001353100000000000001118
000000001306300000000000001080
000000001088600000000000000900
000000001000400000000000000827
000000001034500000000000000855
000000018798400000000000015536
000000005204400000000000004301
000000002292400000000000001895
000000002865600000000000002368
000000000973700000000000000805
000000001002200000000000000828


Please advise.

Ashu
# 2  
Old 05-05-2008
You aren't doing homework, are you?

man bash

look for ${var:n:n}
# 3  
Old 05-05-2008
Try awk
Code:
awk '{print $0(substr($0,1,17)+substr($0,18,13))}' data.file

# 4  
Old 05-06-2008
Danmero,

Thanks for your rpeloy but it doesn't seem to work.
Can you please look into it.

Thanks.

Last edited by er_ashu; 05-06-2008 at 12:53 AM..
# 5  
Old 05-06-2008
Quote:
Originally Posted by er_ashu
Hi All,

Here is the problem:

The input file is like as per below. Each record has 30 chars in total. Have to add the first 17 and the next 13 and append the output. Total records can vary.

000000004728800000000000003908
000000003005100000000000002484
000000002602200000000000002151
000000003418400000000000002825
000000001059900000000000000876
000000001146200000000000000947
000000000973700000000000000805
000000001561300000000000001290
000000001088600000000000000900
000000001146200000000000000947
000000001029300000000000000851
000000001034500000000000000855
000000000977700000000000000808
000000000888800000000000000735
000000000884200000000000000731
000000001061100000000000000877
000000002721600000000000002249
000000001040800000000000000860
000000001353100000000000001118
000000001306300000000000001080
000000001088600000000000000900
000000001000400000000000000827
000000001034500000000000000855
000000018798400000000000015536
000000005204400000000000004301
000000002292400000000000001895
000000002865600000000000002368
000000000973700000000000000805
000000001002200000000000000828


Please advise.

Ashu
Code:
#!/usr/bin/ksh
for line in `cat sample`
do
first17=`echo $line | cut -c1-17`
next13=`echo $line | cut -c18-30`
sum=$(($first17 + $next13))
echo $sum
done

# 6  
Old 05-06-2008
Namish,

Thansk for your reponse but I want the follwoing.

file.txt

111111111111111112000000000000
111111111111111112000000000000
111111111111111112000000000000
111111111111111112000000000000


output should be

444444444444444448000000000000

Sorry for any confusion.

Thanks
Ashu
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Summing a number column

hi All, i have a file in which only one column is there., test.txt ====== -900.01 -900.02 -900.03 -900.04 -900.05 -900.06 -900.07 -900.08 -900.09 900.01 900.02 900.03 900.04 900.05 (4 Replies)
Discussion started by: mechvijays
4 Replies

2. Shell Programming and Scripting

Summing all fields in a file

I was playing around to see how stuff works, and was trying to sum all fields in a file. cat file 1 2 3 4 5 6 7 8 9 10 11 12 I made this script: awk 'BEGIN {OFS=RS}{$1=$1}{s+=$0} END {print "sum="s}' file This gives 15, why not 78? I test it like this awk 'BEGIN... (5 Replies)
Discussion started by: Jotne
5 Replies

3. Shell Programming and Scripting

Help summing a file using awk

I'm trying to sum a text file using AWK. Here is an example of the file: 600|3H68| 46 600|3H69| 46 600|3H6F| 290 600|3H6G| 24 600|3HDY| 1 600|3HDY| 3 600|3HE0| 1 600|3HE0| 3 I would like to sum the third field if the first... (7 Replies)
Discussion started by: Drenhead
7 Replies

4. Shell Programming and Scripting

Using awk to summing from a given line

My file is something like this : 03.097 03.094 03.093 03.095 03.091 04.089 06.093 07.225 08.196 06.097 06.094 05.096 04.086 I'd like to sum it from a given line to another one , e.g.: from line 10 until line 20 What s the awk way solving this ? (1 Reply)
Discussion started by: firelink
1 Replies

5. Shell Programming and Scripting

summing from two different files

I have two files hhhh 3674.00 a 75 1535 183 2134 291 2452 442 2738 704 3048 a 1007 3549 1282 4413 1494 5001 1631 5217 1954 5610 a 2540 5832 3248 6080 3629 6264 4851 6600 7004 6985 ... (4 Replies)
Discussion started by: Indra2011
4 Replies

6. Shell Programming and Scripting

Summing values in columns

Basically I have to process a text file which has been sorted this way: John 12 John 13 John 10 John 900 Peter 20 Peter 30 Peter 32 The first column is a name, and the second an arbitrary value, both delimited by a space. How can I sum them up such that it would become: John 935... (2 Replies)
Discussion started by: Dwee
2 Replies

7. Shell Programming and Scripting

Summing amounts

Hi I need to sum of sub total amounts. Please any ideas file1.txt type: xx xyz 200 300 xyz1 300 400 Sub total 500 700 type:yy abc 200 300 abc1 100 100 Sub total ... (4 Replies)
Discussion started by: mohan705
4 Replies

8. Shell Programming and Scripting

Summing the columns of a file

Hi All, I have a file like - num.txt 12, 34, 65, line1 34, 65, 89, line2 43, 65, 77, line3 I want to do two things - 1. Add first three columns of each line and print the line with largest value. i.e. (12+34+65) for 1st line and so on. 2. Add middle column of each line i.e.... (3 Replies)
Discussion started by: asahlot
3 Replies

9. UNIX for Dummies Questions & Answers

summing according to the column

I have a text file with two columns the first column is an integer and the second column is date how do i sum up the first column according to the date example 123 jan1 232 jan1 473 jan2 467 jan2 356 jan3 376 jan3 my result should be 355 jan1 940 jan2 732 jan3 how do i... (2 Replies)
Discussion started by: ramky79
2 Replies

10. Shell Programming and Scripting

Summing on column

Hi Friends How to do sum on a column? I have a file like: FRED 500.01 TX SMITH 50.10 NY HARRY 5.00 CA 555.11 Sum on second column. I am trying using nawk like nawk 'BEGIN {FS="|"}; {printf $1"+"}' Thanks a lot for your help S :) (2 Replies)
Discussion started by: sbasetty
2 Replies
Login or Register to Ask a Question