The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding columns to a file figaro UNIX for Dummies Questions & Answers 5 07-20-2008 10:50 PM
Perl: adding columns in CSV file with information in each dolo21taf Shell Programming and Scripting 1 03-04-2008 11:52 PM
Adding columns to excel files using Perl dolo21taf Shell Programming and Scripting 1 02-20-2008 04:13 AM
Adding columns of two files chandra321 Shell Programming and Scripting 6 04-06-2007 06:36 AM
adding columns Kelam_Magnus Shell Programming and Scripting 9 01-25-2002 07:35 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-27-2008
Registered User
 

Join Date: May 2008
Posts: 12
Adding columns in csv

I have the following data in FILE1.CSV:

code: Amount1: Amount2:
xxxxx ,, 200 ,,400
yyxxa ,,200
bbcgu ,,2500 ,,300

i want to be able to produce the following FILE2.CSV:

code: Amount
xxxxx ,, 600
yyxxa ,,200
bbcgu ,,2800

Just want to now how to add the columns in unix and if there is no amount2 then do nothing

thanks for you assistance
Reply With Quote
Forum Sponsor
  #2  
Old 06-27-2008
Moderator
 

Join Date: Sep 2007
Location: Germany
Posts: 1,031
Code:
awk -F",," '
   $0 !~ /Amount/ {print $1,",,"($2+$3)}
   /Amount/ {print "code: Amount"}
' infile
or written on one line:
Code:
awk -F",," '$0 !~ /Amount/ {print $1,",,"($2+$3)}; /Amount/ {print "code: Amount"}' infile
Reply With Quote
  #3  
Old 06-27-2008
Registered User
 

Join Date: May 2008
Posts: 12
OK that does the trick
could you just help explain the code for me as to what each section is doing so that i may learn from this process rather thangettingthe answer.

Also one problem i am finding is that if the original file had a figure of
,,00003864096.93 it is being displayed as
,,3.8641e+06.

It is imperative that the ammount remans the same and no rounding is done, could you assist.
Reply With Quote
  #4  
Old 06-27-2008
Moderator
 

Join Date: Sep 2007
Location: Germany
Posts: 1,031
I didn't check what it does with such long values.

For the code:

Setting awk's input field separator to something useful. Those double commas suited well:
Code:
-F",,"
Code:
   $0 !~ /Amount/ {print $1,",,"($2+$3)}
$0 -> the whole line
!~ -> is not like
/Amount/ -> is just a pattern, a string in that $0
The curly brackets start the action when "the line has nothing like "Amount" in it
print $1 -> prints the 1st field, which is defined by our separator we have set in the beginning
The comma separating $1 and ",," is putting in the OFS, output field separator which is a blank per default
",," -> simply printing two commas
($2+$3) -> adding the values of field 2 and field 3 (don't forget, the ",," counts as field separator and is left out
Thats for that action so far, so we check the current line with the next pattern/action pair

Code:
/Amount/ {print "code: Amount"}
/Amount/ {print "code: Amount"} -> if pattern "Amount" is somewhere in the line, just print out the string "code: Amount"

' infile -> hands over awk the file parse, so no "cat infile| awk ..." is needed, which is useless use of cat.
Reply With Quote
  #5  
Old 06-27-2008
Registered User
 

Join Date: May 2008
Posts: 12
Thanks again, is there anyomne outhere who can assist with the rounding issue?
Reply With Quote
  #6  
Old 06-27-2008
Moderator
 

Join Date: Sep 2007
Location: Germany
Posts: 1,031
Just checked, you can write it like following:

Code:
....int($2+$3)....
or any more suitable type.

Or check this:

The GNU Awk User's Guide
Reply With Quote
  #7  
Old 06-27-2008
Registered User
 

Join Date: May 2008
Posts: 12
thanks you are a star
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 02:19 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0