SUM semicolon-seperated values from txt-file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SUM semicolon-seperated values from txt-file
# 1  
Old 03-02-2015
SUM semicolon-seperated values from txt-file

Hello,

(I'm a shell beginner)
how can I sum the bold values of the following txt-file (values.txt) with bash shell. The result of the sum should be written in a new txt file (sum.txt):

Quote:
0;tables;19;02;2015;E;Z; 2.00;
1;tables;19;02;2015;E;ZEW; 137.10;
0;tables;01;02;2015;E;HAS; 37.50;
...

Thanks in advance

Last edited by milu; 03-02-2015 at 11:18 AM..
# 2  
Old 03-02-2015
Hello Milu,

Welcome to forum, please use code tags for the commands/inputs/codes you are using in your posts as per forum rules. Following may help you in same.
Code:
awk '{A=A?A + $NF+0:$NF+0} END{printf "%.2f\n", A > "NEW_SUM.txt"}'  Input_file

It will create a file named NEW_SUM.txt with the total of the last field.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 03-02-2015
another solution

Code:
awk -F";" '{ sum+=$(NF-1)} END {print sum}' Input_file > Sum.txt

This User Gave Thanks to senhia83 For This Post:
# 4  
Old 03-02-2015
thank you very much. This code works great.
Now I want to filter the sum. Only Lines with the correct Date (=02;2015) should be used for sum.


Quote:
0;tables;19;02;2015;E;Z; 2.00;
1;tables;19;03;2015;E;ZEW; 137.10;
0;tables;01;02;2015;E;HAS; 37.50;
Quote:
Result: 39.50

Last edited by milu; 03-02-2015 at 11:31 AM..
# 5  
Old 03-02-2015
Try

Code:
awk -F";" '$4=="02"{ sum+=$(NF-1)} END {print sum}' Input_file > Sum.txt

Please decide what your requirement is before posting, anyways,, for your latest req
Code:
awk -F";" '$4$5=="022015"{ sum+=$(NF-1)} END {print sum}'


Last edited by senhia83; 03-02-2015 at 11:35 AM..
This User Gave Thanks to senhia83 For This Post:
# 6  
Old 03-02-2015
Hello milu,

Following may help you in same too.

Code:
awk -F";" '{if($4==2){A=A?A + $(NF-1)+0:$(NF-1)+0}} END{printf "%.2f\n", A > "NEW_SUM.txt"}'  Input_file

EDIT: Or following may also work as above.
Code:
awk -F";" '{A=$4==2?A + $(NF-1)+0:$(NF-1)+0} END{printf "%.2f\n", A > "NEW_SUM.txt"}' Input_file


Thanks,
R. Singh

Last edited by RavinderSingh13; 03-02-2015 at 11:45 AM.. Reason: Added one more solution
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Seperated a Column from 'ESC' Character seperated file

Hi Experts I have an escape seperated fields in the unix file. And in the below format file I need to extract the first column. Please help its urgent. cat -v op.dat | head 24397028^ I want to extract the file in below format ( with only first column ) 24397028 2439707 thanks. ... (6 Replies)
Discussion started by: neha_suri06
6 Replies

2. Shell Programming and Scripting

Replace semicolon within double quotes in a file with semicolon delimiter

Hello Team, Could you please help me with the below question? I have a file with the following properties 1) File Delimiter is ; 2) Text columns are within double quotes 3) Numeric columns will not have double quotes 4) File has total 6 columns Please see a sample record from file ... (3 Replies)
Discussion started by: sam99
3 Replies

3. Shell Programming and Scripting

Get the sum of values in between begin and end in the file

Hi All, test file Begin Script Run at Thu Mar 14 09:24:16 PDT 2013 tst_accounts: ws zip: WS_out_20130313.tar.gz dat: test_20130313.dat count: 63574 loaded: xx pre-merge: xx post-merge: xx timestamp: Thu Mar 14 09:30:42 PDT 2013 tst_accounts: ws zip: WS_out_20130313.tar.gz dat: s_20130313.dat... (6 Replies)
Discussion started by: bmk
6 Replies

4. Shell Programming and Scripting

Taking sum up all values inside the file

Hi, Taking sum up all values inside the file by using the below command: paste -sd+ filenmae | bc Getting some error like "0705-001: building space exceeded on line1 stdin" The original data looks like SPACE SPACE SPACE 0.123 JOBNAME1 SPACE SPACE 20.325 JOBNAME2 SPACE SPACE... (2 Replies)
Discussion started by: NareshN
2 Replies

5. Shell Programming and Scripting

Convert comma seperated file to line seperated.

Hi, I have data like this. 1,2,3,4 Output required: 1 2 3 4 I am trying to use tr function but getting error. Help is appreciated. (6 Replies)
Discussion started by: pinnacle
6 Replies

6. Shell Programming and Scripting

Compare semicolon seperated data in 2 files using shell script

hello members, I have some data ( seperated by semicolon ) with close to 240 rows in a text file temp1. temp2.txt stores 204 rows of data ( seperated by semicolon ). I want to : Sort the data in both files by field1.i.e first data field in every row. compare the data in both files and print... (6 Replies)
Discussion started by: novice82
6 Replies

7. Shell Programming and Scripting

Removing blank lines from comma seperated and space seperated file.

Hi, I want to remove empty/blank lines from comma seperated and space seperated files Thanks all for help (11 Replies)
Discussion started by: pinnacle
11 Replies

8. Shell Programming and Scripting

Comma Seperated List of Values

Hi, I have a comma seperated list of values: export list="red,blue,white,yellow" Given a value in a variable "look", i want to check whether the value is available in the above list. But the result should be based on exact string match and not part of the string. I am using following... (9 Replies)
Discussion started by: brap45
9 Replies

9. UNIX for Advanced & Expert Users

How to load comma seperated values file (*.csv) into Oracle table

Hi all I need to input values in a .csv file into my Oracle table running in Unix, I wonder what would be the command to do so... The values are recorded in an excel file and I tried using a formatted text file to do so but failed because one of the field is simply too large to fit in the... (4 Replies)
Discussion started by: handynas
4 Replies

10. UNIX for Dummies Questions & Answers

How to load comma seperated values file (*.csv) into Oracle table

Hi all I need to input values in a .csv file into my Oracle table running in Unix, I wonder what would be the command to do so... The values are recorded in an excel file and I tried using a formatted text file to do so but failed because one of the field is simply too large to fit in the... (5 Replies)
Discussion started by: handynas
5 Replies
Login or Register to Ask a Question