shell script add each row value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script add each row value
# 8  
Old 01-27-2009
Quote:
Originally Posted by pradeep_script
Hi,

I tried this, got it while search.

awk '{sum += $1} END {printf "%.2f\n", sum}' file..it gave the ans, but do not know if it correct..

vgersh99, can you explain the command?
we convert all 'new line' ($) characters to '+' in reality 'flattening' the file like so:
Code:
1
2
3

becomes
Code:
1+2+3+

You notice a 'trailing' '+'? That's last new line. So we suffix the result with '0' - adding '0' to the final. So in our example it becomes:
Code:
1+2+3+0

We pipe the result (using 'echo') into 'bc' to perform the actual math.
Quote:
Originally Posted by pradeep_script
is this a oneline command for adding the values in rows in file?
yes -trying it would help answering the question
Quote:
Originally Posted by pradeep_script
rwuerth, I put the typeset in script,

But it gaves the same error, but decimal numbers are missing in error.
# 9  
Old 01-27-2009
MySQL shell script add each row value

vgersh99, rwuerth

awk '{sum += $1} END {printf "%.2f\n", sum}' file
-----
echo $(sed -e 's/$/+/' /tmp/cc) 0 | bc

Both command works.

thank you for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read one row at time using shell script?

Hello Team, I need information on how to read one row at time using shell script. For example i have below data. service-description servername warning critical mountpoint disk-usage-tmp generic-service test1 80 90 /tmp disk-usage-var ... (6 Replies)
Discussion started by: ghpradeep
6 Replies

2. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

3. Shell Programming and Scripting

Change col to row using shell script..Very Complex

Hi guys I have file A with Below Data ABC123 X1 X2 X3 ABC123 Y1 Y33 Y4 ABC123 Z1 ZS2 ZL3 ABC234 P1 PP3 PP9 ABC234 Q1 ABC234 R1 P09 PO332 PO331 OKI12 .. .. .. Now I want file B as below ABC123 X1 X2 X3;Y1 Y33 Y4;Z1 ZS2 ZL3 ABC234 P1 PP3 PP9;Q1;R1 P09 PO332 PO331 OKI12... (1 Reply)
Discussion started by: asavaliya
1 Replies

4. Shell Programming and Scripting

How to arrange xml tags in single row using shell script?

I want to put one xml record in one row and so on... sample two records are here. <?xml version="1.0"?> <Object> <Header> <XCOMVers>V1.0</XCOMVers> <REPORT>XXXXX</REPORT> <CODE>002</CODE> </Header> <IssueCard> <Record> <L>CAR SYSTEM -SSSSS -</L> ... (1 Reply)
Discussion started by: sene_geet
1 Replies

5. Shell Programming and Scripting

Want to remove the last characters from each row of csv using shell script

Hi, I've a csv file seperated by '|' from which I'm trying to remove the excess '|' characters more than the existing fields. My CSV looks like as below. HRLOAD|Service|AddChange|EN PERSONID|STATUS|LASTNAME|FIRSTNAME|ITDCLIENTUSERID|ADDRESSLINE1 10000001|ACTIVE|Testazar1|Testore1|20041|||... (24 Replies)
Discussion started by: rajak.net
24 Replies

6. UNIX for Dummies Questions & Answers

Shell Script: Traverse Database Table Row by Row

Hello Everyone, My issue is that I want to traverse a database table row by row and do some action on the value retrieved in each row. I have gone through a lot of shell script questions/posts. I could find row by row traversal of a file but not a database table. Please help. Thanks &... (5 Replies)
Discussion started by: ahsan.asghar
5 Replies

7. Shell Programming and Scripting

Spliting a row to multiple rows using shell script

Please advice script for changing from A to B I'd like to Split a row to multiple rows with 4 columns using shell script. The data of the file is in one row as below that is 28Mbyte size. A> cto10001 0000000 201010 10:52:13 cto10001 0000000 201011 10:52:13 cto10001 0000000 201011 10:52:13... (2 Replies)
Discussion started by: ianpapa
2 Replies

8. Shell Programming and Scripting

Script shell on line and row

Hello, I want to make a script to do this : the file is with line : TOTO TEST1 TOTO TEST2 TITI TEST1 TITI TEST2 TITI TEST3 i want he become : TOTO TEST1 TEST2 TITI TEST1 TEST2 TEST3 (3 Replies)
Discussion started by: safsound
3 Replies

9. Shell Programming and Scripting

How to do row comparison in shell script

Hi, I need help on doing the below thing in shell script. I have a file with millions of rows called "abc.txt". i have another file with millions of rows called "xyz.txt". I would like to do the below operation. Open the abc.txt, read the first line, do some operations on the column... (2 Replies)
Discussion started by: informsrini
2 Replies

10. Shell Programming and Scripting

Add value of each row when each row has different data ype

Guys -- I am noob and I am really strugging on this one. I cant even write the pseudo code for it. I have a file that spits values in individual rows as such: file: user date type size joe 2005-25-09 log 1 joe 2005-25-09 snd 10 joe 2005-25-09 rcd 12 joe 2005-24-09 log 2 joe... (1 Reply)
Discussion started by: made2last
1 Replies
Login or Register to Ask a Question