The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Special Forums > Filesystems, Disks and Memory
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


Filesystems, Disks and Memory Questions involving NAS, SAN, RAID, Robotic Libraries, backups, etc go here.


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding columns to a file figaro UNIX for Dummies Questions & Answers 4 1 Week Ago 05:38 AM
have a file with 300 columns dummy_needhelp Shell Programming and Scripting 4 11-04-2007 09:08 PM
How do manipulate file path and names siegfried Shell Programming and Scripting 2 09-28-2007 08:20 AM
How can i replace certain columns in the file mani_um Shell Programming and Scripting 6 06-22-2007 07:40 AM
Appending columns on a file abel Shell Programming and Scripting 2 09-27-2002 04:04 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-04-2006
Registered User
 

Join Date: Dec 2006
Posts: 4
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
manipulate csv file to add columns

Hi, I have a csv file with a key composed by 3 columns and some other numeric fields and I need to obtain the partial amounts by some part of the key. This may be some difficult to understand, so better see an example, where my input file is:

name,surname,department,y2004,y2005,y2006
John,Smith,sales,300,200,100
Mary,Grant,sales,100,0,100
John,Smith,IT,0,50,250
George,Parker,IT,200,150,250
Sarah,Lee,marketing,300,400,100
John,Smith,marketing,200,0,250

And would like to obtain an output file with the total amounts added by department like:

department,y2004,y2005,y2006
sales,400,200,200
IT,200,200,500
marketing,500,400,350

If you imagine this is a database table, this could easily be done in sql with a group by, but unix scripting is something different... Could anyone help me with this?
Thanks a lot.

Oscar.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-04-2006
Registered User
 

Join Date: Jan 2006
Posts: 24
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
awk -F, 'NR==1 {head=$0;next}
{ y4[$3]+=$4;y5[$3]+=$5;y6[$3]+=$6}
END {
print head;
print "sales,"y4["sales"],y5["sales"],y6["sales"];
print "IT," y4["IT"],y5["IT"],y6["IT"];
print "marketing," y4["marketing"],y5["marketing"],y6["marketing"];
}' <data.txt

John Arackal
Reply With Quote
  #3 (permalink)  
Old 12-04-2006
Registered User
 

Join Date: Dec 2006
Posts: 4
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Hi John, thanks for your quick reply, I hardly had time to test my first awk programs... Just a thing, what if you don't know which are going to be the departments? Though, the department data is going to be grouped as in the example.
Oscar.
Reply With Quote
  #4 (permalink)  
Old 12-05-2006
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,340
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Untested, and untidy

Code:
awk -F, 'NR==1 {  printf "%15s", $3
                         for ( i = 4 ; i <= NF; i++ ) { 
                             printf "%8s", $i 
                         }
                         printf "\n"
                         next
                      }                                               
                     {  ind[$3] = NF 
                         for ( i = 4 ; i <= NF; i++ ) {
                             result[$3,i] += $i 
                         }
                      }
            END {
                      for ( j in ind ) { 
                         printf "%15s",j 
                         for ( i = 4 ; i <= ind[j]; i++) { 
                             printf "%8d", result[j,i]
                         }
                         print line}
                   }' datafile
Reply With Quote
  #5 (permalink)  
Old 12-06-2006
Registered User
 

Join Date: Dec 2006
Posts: 4
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Hi, I could manage with that,
thanks a lot.
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB 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 -7. The time now is 10:28 PM.


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

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102