Format csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format csv file
# 1  
Old 09-07-2012
Format csv file

Hi,

I need to make some changes in a csv file using awk or perl. Unfortunately, all my attempts have led to nothing so I hope you guys can help me.

I have the following example input file including header(original file has 35 fields):
Code:
ABC: DE
Time: 2012/09/07
,"Notional","Name","POS","TEST;Field1","TEST;Field2"
"TestField1",0.0000 XYZ,NameField,PosField,"0.00 XYZ","0.00 XYZ"
"TestField2",0.0000 XYZ,NameField,PosField,"0.00 XYZ","0.00 XYZ"

The first column is called "PosId", but the name is missing in the header.

I need to format this to have the following output:
Code:
"Date","PosId","Name","POS","ScenSet","TEST_Field1","TEST_Field2"
2012/09/07,TestField1,NameField,PosField,DE,0.00 XYZ,0.00 XYZ
2012/09/07,TestField2,NameField,PosField,DE,0.00 XYZ,0.00 XYZ

So basically, I need to do the following:
- Take the second field of the first line (DE) and put it in the 5th column (ScenSet)
- Take the second field of the second line (2012/09/07) and put it in the first column (Date)
- Remove the Notional field
- Replace ";" by "_" in the header for the fields (TEST;Fieldx)

Thanks
# 2  
Old 09-07-2012
did it in hurry u can cut corners in this command to make it look small Smilie

Code:
 
awk -F"[,:]" 'NR==1{var1=$2};NR==2{var2=$2};NR==3{gsub(/;/,"_");$1="\"Date\"";$2="\"PosId\"";OFS=",";print};NR>3{$5=var1;gsub(/"/,"");OFS=",";print var2","$0}' filename

This User Gave Thanks to vidyadhar85 For This Post:
# 3  
Old 09-10-2012
Thanks for the solution vidyadhar85, it works great in this case.
You use both ",:" as a field separator, but what if the ":" character is in any of the 3rd-end lines? They get an extra field.

Is it possible to tell awk to use a specific separator for the first 2 fields, then change the separator to an other character?

EDIT: Nvm, found it. Just use FS="" inline instead of -F""

Last edited by Subbeh; 09-10-2012 at 06:13 AM..
# 4  
Old 09-12-2012
There is a problem with the code. The first 5 columns get overwritten with the new columns. The new columns need to be inserted before the original columns.

Do you have any idea of how to do this?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

2. UNIX Desktop Questions & Answers

Format csv file using Unix

Hi All, I have an csv file with three rows, where first containing header deatils. is there any way to make the first row to appear bold using UNIX command. Input File: Name Rank arun 1 babu 2 Expected Output: Name Rank arun 1 babu 2 (7 Replies)
Discussion started by: arunmanas
7 Replies

3. Shell Programming and Scripting

Convert the below file to csv format

Hi , i want to change this question, i will post soon.. (6 Replies)
Discussion started by: srikanth2567
6 Replies

4. Shell Programming and Scripting

format output in csv file

I am sending the output of a file to .csv file. The output should look like this: Total Customers Processed:,8 Total Customers Skipped:,0 Total Customers Added:,8 Total Customers Changed:,0 Total Policies Deleted:,0 Total Policies Failed:,0 total:,8 Now i want this output in... (1 Reply)
Discussion started by: Prashant Jain
1 Replies

5. Shell Programming and Scripting

Format CSV file

I have a csv file which I need to process and export back to xlsx file. For instance, the csv contains: John Smith, job-title, hours John Smith, job-title, hours Mary Smith job-title, hours etc. I need to import that to a script, get id of redundant data i.e: John smith, job-title,... (13 Replies)
Discussion started by: _tina_
13 Replies

6. Shell Programming and Scripting

Format txt file to CSV

Hi All, I have a file with content FLIGHT PLANS PRODUCED ON 26.08.2008(SORTED BY FPLAN NUMBER) RUN DATED 27/08/08 PAGE 1 -------------------------------------------------------------- FPLAN FPLAN PRE BTCH BATCH POST BTCH BATCH BATCH ... (1 Reply)
Discussion started by: digitalrg
1 Replies

7. Shell Programming and Scripting

Format a date in a csv file

So I have a csv file where the 3rd field is a date string in the format yyyy-mm-dd. I need to change it to mm/dd/yyyy. So each line in the csv file looks like: StringData,StringData,2009-02-17,12.345,StringData StringData,StringData,2009-02-16,65.789,StringData Any idea how I can keep... (5 Replies)
Discussion started by: rpiller
5 Replies

8. Shell Programming and Scripting

changing the format of CSV file

Hi Experts, Please help me to get the following from script for Unix ENvironment(shell, perl, tr, sed, awk). INPUT FILE: 20K,ME,592971 20K,YOU,2 20K,HE,1244998 50K,YOU,480110 50K,ME,17 50K,HIS,10 50K,HE,1370391 OUTPUT FILE: K,ME,YOU,HE,HIS 20K,592971,2,1244998,0... (5 Replies)
Discussion started by: ashis.tewari
5 Replies

9. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies

10. UNIX for Advanced & Expert Users

How to Parse a CSV file into a Different Format

Hi I have a CSV file with me in this format Currency, USD, EUR, USD, 1.00, 1.32, EUR, 0.66, 1.00, How do I transpose the file to get to the format below. currency, currency, rate USD, USD, 1.00 USD, EUR, 1.32 EUR, USD, 0.66 EUR, EUR, 1.00 Thanks for your help We are using... (2 Replies)
Discussion started by: cdesiks
2 Replies
Login or Register to Ask a Question