Manipulate a CSV File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulate a CSV File
# 1  
Old 02-05-2014
[Solved] Manipulate a CSV File

Hello,

How do i manipulate .csv file to this format?

Thank you very much.

Source:
Code:
john,5
marco,7
john,4
paul,3
marco,8

Output:
Code:
john,9
marco,15
paul,3

# 2  
Old 02-05-2014
Code:
$ awk -F, '{ A[$1]+=$2 } END { OFS=","; for (x in A) print x,A[x]; }' input >output
paul,3
john,9
marco,15

# 3  
Old 02-05-2014
What have you tried so far?
# 4  
Old 02-05-2014
Quote:
Originally Posted by hergp
Code:
$ awk -F, '{ A[$1]+=$2 } END { OFS=","; for (x in A) print x,A[x]; }' input >output
paul,3
john,9
marco,15

Hi,

it gives an error on centos
# 5  
Old 02-05-2014
What is the error message? It works here on CentOS 6.5:
Code:
$ cat input
john,5
marco,7
john,4
paul,3
marco,8
$ awk -F, '{ A[$1]+=$2 } END { OFS=","; for (x in A) print x,A[x]; }' input
paul,3
john,9
marco,15
$ cat /etc/centos-release
CentOS release 6.5 (Final)
$ uname -a
Linux xxxxx 2.6.32-431.3.1.el6.i686 #1 SMP Fri Jan 3 18:53:30 UTC 2014 i686 i686 i386 GNU/Linux
$ type awk
awk is a tracked alias for /bin/awk

This User Gave Thanks to hergp For This Post:
# 6  
Old 02-05-2014
ok its works now Smilie thank you very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to manipulate csv file in UNIX

Hi, I am new to awk and unix programming and trying to manipulate a csv file. My current csv file looks like this: col1,col2,col3,col4,col5,col4,col5,col6,col7,col8 223,V,c,2,4,f,r,,y,z 223,V,c,3,2,f,r,,y,z 223,V,c,1,4,f,r,,y,z 223,V,c,4,3,f,r,,y,z 227,V,c,3,1,f,r,,y,z... (8 Replies)
Discussion started by: Prit Siv
8 Replies

2. Shell Programming and Scripting

Save output of updated csv file as csv file itself, part 2

Hi, I have another problem. I want to sort another csv file by the first field. result.csv SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw /home/intannf/foto5/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 ... (2 Replies)
Discussion started by: refrain
2 Replies

3. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

4. Shell Programming and Scripting

Manipulate the text file in UNIX

Hi All, I have a file like below and i have 2 questions on this (They are 3 lines starts with 01 , 02 and 03. but is 01abc333644554 234 2334535 34534535355353 sfsdf345455 353 4543 jgkg tty 7676 02cdesdfsdfsdf 234 wesdfsdf 345345 234234 234234 2342342 dfgdfg sdfgg dgdgdg fgvfs... (6 Replies)
Discussion started by: siva.pitchai
6 Replies

5. Shell Programming and Scripting

awk command to manipulate csv file in UNIX

Hi, I am new to awk/unix and am trying to put together a script to manipulate the date column in a csv file. I have file1.csv with the following contents: Date,ID,Number,Amount,Volume,Size 01-Apr-2014,WERFG,998,105873.96,10873.96,1342.11 01-Apr-2014,POYFR,267,5681.44,5681.44,462.96 I... (2 Replies)
Discussion started by: Prit Siv
2 Replies

6. Shell Programming and Scripting

Manipulate file

Hi Guys, I have a file that lists patches along with other information. The patches are listed in two different formats. One format lists the latest patch, date , installed patch Latest Patch Date IN 148412-02 13-Sep-2012 -- X X SunOS 5.10: nss_dns patch 126206-10 ... (5 Replies)
Discussion started by: Tornado
5 Replies

7. UNIX for Dummies Questions & Answers

Manipulate and move columns in a file

Hello Unix Gurus, I have a request 2 perform several functions on a file, delete columns, delete rows based on column value, and finally move around columns in the final output. Consider the following input file with 12 columns; ... (1 Reply)
Discussion started by: chumsky
1 Replies

8. Shell Programming and Scripting

Shell script to manipulate a file

Hello, I have a file with following contents : WSL SRVGRP=LISTENER SRVID=2 CLOPT="-A -t -- -n 0x0002aa050a03cc65 " RQPERM=0660 REPLYQ=Y RPPERM=0660 MIN=1 MAX=1 CONV=N I need to print only the value in Hex i.e.... (2 Replies)
Discussion started by: deo_kaustubh
2 Replies

9. Filesystems, Disks and Memory

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... (6 Replies)
Discussion started by: oscarmon
6 Replies
Login or Register to Ask a Question