The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unix addition ( Row wise) gauravgoel Shell Programming and Scripting 3 05-17-2007 04:27 AM
linux setup and addition config chlawren Linux 3 02-05-2007 11:10 AM
floating point addition ravi raj kumar Shell Programming and Scripting 8 12-22-2006 02:47 AM
Addition of numbers in unix asinha63 Shell Programming and Scripting 3 03-14-2006 12:38 PM
suggested addition to forum rules PxT Post Here to Contact Site Administrators and Moderators 1 10-31-2001 09:51 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-07-2007
onlyroshni onlyroshni is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 8
Addition problem

Hello Seniors!!! I am trying to add to lines on a file which is delimited by character "|".

input.txt
Quote:
Line Count|0|0|173|1406|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|2 46|0|0|0|46|0|619
Line Count|1|5|0|0|0|0|0|0|0|0|0|33|12|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0| 0|0|0|0|0
Desired Output file should give simple addition.
output.txt
Quote:
Line Count|1|5|173|1406|0|0|0|0|0|0|0|33|12|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0 |246|0|0|0|46|0|619
Can some one provide me simple awk solution to this. M struggling to solve this using awk & for loop inside awk.

Thanks in advance.
  #2 (permalink)  
Old 12-07-2007
gus2000 gus2000 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 157
You didn't say how many lines (records) there were, but this should work for any number of lines:


Code:
# echo "1|2|3|4|5
2|2|2|2|2" | awk '
{
  n=split($0,a,"|")
  for (i=1;i<=n;i++) { sum[i]=sum[i]+a[i] }
}END{
  for (i=1;i<n;i++) { printf "%d|",sum[i] }
  print sum[n]
}'

3|4|5|6|7
If you need to skip summing the first field (which you apparently do) then start "i=2" (both places), and then add "printf "%s|",a[1]" before the final "for" loop.

Last edited by gus2000; 12-08-2007 at 07:00 PM..
  #3 (permalink)  
Old 12-10-2007
onlyroshni onlyroshni is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 8
Smile Thanks.

Thanks this works for me. I have only 2 lines.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0