awk sum entire string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk sum entire string
# 1  
Old 02-21-2013
awk sum entire string

Hi I am trying to carry out a sum on a file (totals.txt).

The file looks like:
Quote:
000000027300
000000178800
So far i have this command
Quote:
awk '{ sum += $1 } END { print sum }' totals.txt
this returns 20610

I however want it to return 000000206100

Any help would be great
thanks!
# 2  
Old 02-21-2013
Code:
awk '{ sum += $1 } END { printf "%012d", sum }' totals.txt

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 02-21-2013
Quote:
Originally Posted by balajesuri
Code:
awk '{ sum += $1 } END { printf "%012d\n", sum }' totals.txt

May be you missed a new-line...but then that also depends on the OP's requirements...
This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 02-21-2013
Ah, yes! A silly miss.
# 5  
Old 02-21-2013
Hi Guys,

Thanks for the help.

The awk command now returns
Quote:
000000020610
I need it to return:
Quote:
000000206100
Both numbers in the file end with a number followed by 00.
The Awk command seems to remove the final 0 from the sum.

I should have mentioned that these are actually figures with the decimal point removed so, 000000027300 = 273.00 and 000000178800 = 1788.00 and that is why it is important that the 00 at the end of the total remains.

Apologies for the confusion
# 6  
Old 02-21-2013
Please post the output of head -3 totals.txt|od -bc. totals.txt is your input file name.
# 7  
Old 02-21-2013
head -3 totals.txt|od -bc output =
Quote:
0000000 060 060 060 060 060 060 060 062 067 063 060 060 012 060 060 060
0 0 0 0 0 0 0 2 7 3 0 0 \n 0 0 0
0000020 060 060 060 061 067 070 070 060 060 012
0 0 0 1 7 8 8 0 0 \n
0000032
---------- Post updated at 02:38 PM ---------- Previous update was at 02:18 PM ----------

Sorry Guys,

That awk command provided worked!

There was an error with my file.

Thanks for the help Guys

Last edited by nwalsh88; 02-21-2013 at 10:36 AM..
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: count unique elements in a field and sum their occurence across the entire file

Hi, Sure it's an easy one, but it drives me insane. input ("|" separated): 1|A,B,C,A 2|A,D,D 3|A,B,B I would like to count the occurence of each capital letters in $2 across the entire file, knowing that duplicates in each record count as 1. I am trying to get this output... (5 Replies)
Discussion started by: beca123456
5 Replies

2. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

3. Shell Programming and Scripting

Avoid printing entire line if string not found

so im searching the process table with: ps -ef | awk -F"./rello.java" '{ print substr($0, index($0,$2)) }' I only want it to print everything that's infront of the "./rello.java". That's because im basically getting the arguments that was passed to the rello.java script. this works. ... (2 Replies)
Discussion started by: SkySmart
2 Replies

4. Shell Programming and Scripting

Regular Expression - Switch Entire String

I would like to be able to use a regular expression to find and replace entire strings, but not replace if the string is a substring in a larger string. Example: $string = "ABC ABCDEF ABC ABCDEF ABC"; Something like - $string =~ s/ABC/XYZ/g; ->Desired: $string = "XYZ ABCDEF XYZ ABCDEF... (3 Replies)
Discussion started by: rjulich
3 Replies

5. Shell Programming and Scripting

Print entire line only if certain fixed character matches the string

Hi All, I have a file testarun.txt contains the below lines and i want to print the lines if the character positions 7-8 matches 01. 201401011111 201401022222 201402013333 201402024444 201403015555 201403026666 201404017777 201404028888 201405019999 201405020000 I am trying the... (4 Replies)
Discussion started by: Arunprasad
4 Replies

6. Shell Programming and Scripting

Print only matched string instead of entire line

Hi, I have a file whose lines are something like Tchampionspsq^@~^@^^^A^@^@^@^A^A^Aÿð^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^?ð^@^@^@^@^@^@^@?ð^@^@^@^@^@^@pppsq^@~^@#@^@^@^@^@^@^Hw^H^@^@^@^K^@^@^@^@xp^At^@^FTtime2psq^@ ~^@^^^A^@^@^@^B^A I need to extract all words matching T*psq from the file. Thing is... (4 Replies)
Discussion started by: shekhar2010us
4 Replies

7. Web Development

Replacing a string in an entire mysql Database

Hi, Can someone please tell me how I can replace every occurrence of a string with another string, in the entire mysql database? What this means is, i just dont want to operate table by table. I want to search the database and automatically replace the string /opt/cacti/scripts/ with... (1 Reply)
Discussion started by: SkySmart
1 Replies

8. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

9. UNIX for Dummies Questions & Answers

Search for a string and copy the entire line

Hello All, I am after the script or the command which can scan the entire file for a string $PART_ID and when found to extract/copy the corresponding $PART_ID value (e.g THIRE_PTY_SOFTWARE for the 1st occurance of $PART_ID in the attached file) to a file. Appreciate your help. Thanks in... (3 Replies)
Discussion started by: forumthreads
3 Replies

10. Shell Programming and Scripting

sort entire line based on part of the string

hey gurus, my-build1-abc my-build10-abc my-build2-abc my-build22-abc my-build3-abc basically i want to numerically sort the entire lines based on the build number. I dont zero pad the numbers because thats "how it is" ;-) sort -n won't work because it starts from the beginning. ... (10 Replies)
Discussion started by: gurpal2000
10 Replies
Login or Register to Ask a Question