Converting a text file to a csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting a text file to a csv file
# 1  
Old 12-06-2005
Converting a text file to a csv file

I have a text file that is the output of a Netbackup report. The file it generates is just a plain text file with only white space between fields. For example:

Date Policy Type Kilobytes Retention
12/5/2005 WinNT Full 18329948 6 Months


I need to write a script that will make it look like this:


Date,Policy,Type,Kilobytes,Retention
12/5/2005,WinNT,Full,18329948,6 Months

As you can see in the first example, the number of spaces between words aren't uniform, which complicates things a bit.

If anyone has any suggestions, I would appreciate it.
# 2  
Old 12-06-2005
This:
Code:
awk '{
     for(i=1;i<5;i++)
     { printf("%s,", $i); }
     for(i=5;i<=NF;i++)
     {printf("%s ",$i); }
     printf("\n");
     }'           filename

run against this data:
Code:
Date Policy Type Kilobytes Retention  
12/5/2005 WinNT Full 18329948 6 Months
12/5/2005 WinNT Half 100    6 Months
12/5/2005 WinNT Full 200 6 Months

Gave this output:
Code:
kcsdev:/home/jmcnama> t.awk
Date,Policy,Type,Kilobytes,Retention
12/5/2005,WinNT,Full,18329948,6 Months
12/5/2005,WinNT,Half,100,6 Months
12/5/2005,WinNT,Full,200,6 Months

# 3  
Old 12-06-2005
first convert all retention times to use an underscore (or other symbol) instead of a space and then change the spaces in between the columns to commas
# 4  
Old 12-06-2005
or use jim's code Smilie
# 5  
Old 12-06-2005
Jim's code worked perfectly! Thank you Jim!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting csv file to flat file

Hi All, I have a csv file which is comma seperated. I need to convert to flat file with preferred column length country,id Australia,1234 Africa,12399999 Expected output country id Australia 1234 Africa 12399999 the flat file should predefined length on respective... (8 Replies)
Discussion started by: rohit_shinez
8 Replies

2. Shell Programming and Scripting

Converting data for text file to csv

Gents Using the script attached (raw2csv). i use to create the file .csv.. The input file is called 201.raw. Kindly can you check if there is easy way to do it. The script works fine but takes a lot time to process Thanks for your help (8 Replies)
Discussion started by: jiam912
8 Replies

3. Shell Programming and Scripting

Format problem while converting text file to csv

Hi , I need a help in following scenario.I tried searching in google but couldn't able to find the exact answer. Sorry if i am re-posting already answered query. While i am trying to convert into log file into csv i couldn't able to get the format which i am looking for. I converted file... (4 Replies)
Discussion started by: varmas424
4 Replies

4. Shell Programming and Scripting

need to save the space when converting to CSV file

Hi, I have a text file with the following format. Some of the fields are blank. 1234 3456 23 45464 327837283232 343434 5654353 34 34343 3434345 434242 .... .... .... I need to convert this file to a CSV file, like 1234, ,23, ... (3 Replies)
Discussion started by: wintersnow2011
3 Replies

5. UNIX for Dummies Questions & Answers

Converting a text file with irregular spacing into a space delimited text file?

I have a text file with irregular spacing between values which makes it really difficult to manipulate. Is there an easy way to convert it into a space delimited text file so that all the spaces, double spaces, triple spaces, tabs between numbers are converted into spaces. The file looks like this:... (5 Replies)
Discussion started by: evelibertine
5 Replies

6. UNIX for Dummies Questions & Answers

cleaning up spaces from fixed width file while converting to csv file

Open to a sed/awk/or perl alternative so that i can stick command into my bash script. This is a problem I resolve using a combination of cut commands - but that is getting convoluted. So would really appreciate it if someone could provide a better solution which basically replaces all... (3 Replies)
Discussion started by: svn
3 Replies

7. Shell Programming and Scripting

Converting txt file in csv

HI All, I have a text file memory.txt which has following values. Average: 822387 7346605 89.93 288845 4176593 2044589 51883 2.47 7600 i want to convert this file in csv format and i am using following command to do it. sed s/_/\./g <... (3 Replies)
Discussion started by: mkashif
3 Replies

8. UNIX for Dummies Questions & Answers

Converting txt file to csv file

Hi, Using rsync, I've sent the output to a text file. This is the text file : Pls help me on converting this text file to a csv file. Probably a script or sth to convert the text file to a csv file. (3 Replies)
Discussion started by: anaigini45
3 Replies

9. UNIX for Advanced & Expert Users

Converting .csv file into .xls file and send it to inbox

Hi All, I wrote a script to extract data from Oracle DB and place it in a text file , and I have coverted .txt file into comma seperated .csv file and I sent it to my mail box . I can get .xls file in my inbox.I am getting all data in same column and in different rows , without column... (1 Reply)
Discussion started by: krthkmuthu
1 Replies

10. UNIX for Advanced & Expert Users

Problem in converting password protected excel file to csv file in unix

I need to convert a password protected excel file which will be in UNIX server to a comma separated file. For this I need to open the excel file in UNIX box but the UNIX box doesn't prompt for password instead it is opened in an encrypted manner. I could manually ftp the excel file to local... (2 Replies)
Discussion started by: Devivish
2 Replies
Login or Register to Ask a Question