![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Code:
awk -F",," '
$0 !~ /Amount/ {print $1,",,"($2+$3)}
/Amount/ {print "code: Amount"}
' infile
Code:
awk -F",," '$0 !~ /Amount/ {print $1,",,"($2+$3)}; /Amount/ {print "code: Amount"}' infile
|
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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)}
!~ -> 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"}
' infile -> hands over awk the file parse, so no "cat infile| awk ..." is needed, which is useless use of cat. |
|
#5
|
|||
|
|||
|
Thanks again, is there anyomne outhere who can assist with the rounding issue?
|
|
#6
|
|||
|
|||
|
Just checked, you can write it like following:
Code:
....int($2+$3).... Or check this: The GNU Awk User's Guide |
|
#7
|
|||
|
|||
|
thanks you are a star
|
|||
| Google The UNIX and Linux Forums |