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
adding values with a loop hcclnoodles Shell Programming and Scripting 1 05-27-2008 06:42 AM
replace the column values. charandevu Shell Programming and Scripting 6 04-02-2008 02:21 AM
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 07:05 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
replace a column values with the first value in column sumeet UNIX for Advanced & Expert Users 3 02-06-2007 01:13 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-08-2007
berlin_germany berlin_germany is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 17
Help in adding positive & negative values in a column

Hi Gurus,

In my file I have an amount field from position 74 to 87, which contains values starting with '+' as well as '-'. I want to add all positive values in a varible called "CREDIT" and all negative values in a variable "DEBIT". I know, we can use grep to identify values with positive and negative signs. But, don't know, how to add them in 2 separate variables in a script. Is there any way to do that on command line? Any help would be much appriciated. Thanks!

In my file Amount field has right justified values like,

+1345778500000
-327800000
+642307000000
-4300000
+2308007
These values look left justified here on the screen, but they are actually right justified in my file.
  #2 (permalink)  
Old 02-08-2007
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,647
Code:
awk ' {
           value=substr($0,74,14)
           while (substr(value,1,1) == " " ) {value=substr(value,2)}
           if(value>0) { plus+=value}
           else    {minus+=value}
        }
       END { print plus, minus} ' filename | read credit debit
try something like that....

.. messed up the right justification...
  #3 (permalink)  
Old 02-09-2007
berlin_germany berlin_germany is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 17
Thanks jim mcnamara for the reply. It works fine, when values are left justified. But, I have right justified values in the file. So, '+' OR '-' can start at any position from 74 to 87 bytes.

How can we use 'length' function to treat the values as left justified, so the leading spaces can be ignored?
  #4 (permalink)  
Old 02-09-2007
berlin_germany berlin_germany is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 17
I think, following will solve the problem, where I don't care about position of '+' and '-' signs in the values. I can just check values > 0 and the rest, and according to that sum them in 2 different variables as below:

awk ' {
value=substr($0,74,14)
if(value>0) { plus+=value}
else {minus+=value}
}
END { print plus, minus} ' filename | read credit debit
  #5 (permalink)  
Old 02-09-2007
berlin_germany berlin_germany is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 17
Following works fine,

#!/bin/ksh

credit_sum=`awk '{ value=substr($0, 74, 14); if(value>0) {credit+=value}} END {print credit}' filename`

debit_sum=`awk '{ value=substr($0, 74, 14); if(value<0) {debit+=value}} END {print debit}' filename`

exit 0;
Sponsored Links
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 11:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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