awk command to manipulate csv file in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command to manipulate csv file in UNIX
# 1  
Old 09-02-2014
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:

Code:
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 want to format the date column as follows -> yyyy-mm-dd and want the output saved in a tempfile:

Code:
Date,ID,Number,Amount,Volume,Size
2014-04-01,WERFG,998,105873.96,10873.96,1342.11
2014-04-01,POYFR,267,5681.44,5681.44,462.96

Is there a simple awk script to achieve this?

Appreciate the help!!

Thanks,

Last edited by joeyg; 09-02-2014 at 02:19 PM.. Reason: Please wrap code and data in CodeTags
# 2  
Old 09-02-2014
Code:
awk 'BEGIN { months["Jan"] = "01"
             months["Feb"] = "02"
             months["Mar"] = "03"
             months["Apr"] = "04"
             months["May"] = "05"
             months["Jun"] = "06"
             months["Jul"] = "07"
             months["Aug"] = "08"
             months["Sep"] = "09"
             months["Oct"] = "10"
             months["Nov"] = "11"
             months["Dec"] = "12"
             sep="-"
             }
    NR>1   { split($1,a,sep) 
             $1=a[3] sep months[a[2]] sep a[1]
           }1' OFS=\, FS=\, file1.csv > tempfile


Last edited by pilnet101; 09-02-2014 at 04:06 PM..
This User Gave Thanks to pilnet101 For This Post:
# 3  
Old 09-02-2014
save csv in other file

That worked perfectly!! Thank you so much!! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

awk command to retrieve record 23 and 89 from UNIX file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am looking for awk command to retrieve only the record number 23 and record number 89 from a unix file?... (6 Replies)
Discussion started by: rakeshp
6 Replies

2. UNIX for Beginners Questions & Answers

awk command to retrieve record 23 and 89 from UNIX file

Hi Everyone, I am looking for awk command to retrieve only the record number 23 and record number 89 from a unix file? Please let me know what is the awk command for this? Regards Rakesh (1 Reply)
Discussion started by: rakeshp
1 Replies

3. 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

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

Manipulate a CSV File

Hello, How do i manipulate .csv file to this format? Thank you very much. Source: john,5 marco,7 john,4 paul,3 marco,8 Output: john,9 marco,15 (5 Replies)
Discussion started by: tara123
5 Replies

6. Shell Programming and Scripting

UNIX command output in csv format

I'm just wondering is there any way to capture the output of a unix command in a csv format. df -h gives the result of filesystem,free space,Used space, use %,mounted on. Is there a way to capture the command output and format it as comma sparated or fixed length file. (3 Replies)
Discussion started by: anita81
3 Replies

7. UNIX for Dummies Questions & Answers

xml to csv using sed and awk command

Hi Guys, Can you help me in creating shell script using sed,awk etc commands to generate csv file using xml file. (5 Replies)
Discussion started by: sbk
5 Replies

8. UNIX for Dummies Questions & Answers

Question on how to manipulate a SIMPLE text file (using awk?)

I have a simple txt files that looks something like this (The title is a part of the text file) Student Grades --------------- 1 Tim Purser 89 2 John Wayne 56 3 Jenn Hawkins 95 4 Harry Potter 75 Here are my questions: How would I ONLY print the names of students... (2 Replies)
Discussion started by: ninjagod123
2 Replies

9. Shell Programming and Scripting

Using awk command for .csv file

Hi , I have .csv file with value separated by ";". 1. Using awk how to extract perticular colums and store in to array 2. For some columns I needs to extract last value of the column How to do same please help me ASAP Thanks and Regards, Sushma (9 Replies)
Discussion started by: sushmab82
9 Replies

10. 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

Featured Tech Videos