The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Converting .ps file to .pdf file from where it is broken sunitachoudhury UNIX for Advanced & Expert Users 5 04-07-2008 11:13 AM
Converting a Delimited File to Fixed width file raghavan.aero Shell Programming and Scripting 2 06-06-2007 02:44 PM
Converting Pivot file to flat file vskr72 Shell Programming and Scripting 2 10-18-2005 04:41 PM
Converting Text File into XML using Unix Shell Scripts Laud12345 Shell Programming and Scripting 10 02-16-2005 01:35 PM
Converting a .job file Juliette Windows & DOS: Issues & Discussions 6 12-04-2004 02:42 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-06-2005
primowalker primowalker is offline
Registered User
  
 

Join Date: Dec 2005
Location: Nebraska
Posts: 2
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 (permalink)  
Old 12-06-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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 (permalink)  
Old 12-06-2005
Just Ice's Avatar
Just Ice Just Ice is offline Forum Advisor  
Lights on, brain off.
  
 

Join Date: Mar 2005
Location: in front of my computer
Posts: 637
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 (permalink)  
Old 12-06-2005
Just Ice's Avatar
Just Ice Just Ice is offline Forum Advisor  
Lights on, brain off.
  
 

Join Date: Mar 2005
Location: in front of my computer
Posts: 637
or use jim's code
  #5 (permalink)  
Old 12-06-2005
primowalker primowalker is offline
Registered User
  
 

Join Date: Dec 2005
Location: Nebraska
Posts: 2
Jim's code worked perfectly! Thank you Jim!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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

BB 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 -4. The time now is 10:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0