Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-30-2010
Registered User
 

Join Date: Feb 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Converting txt file in csv

HI All,


I have a text file memory.txt which has following values.



Code:
   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.



Code:
  sed s/_/\./g < memory.txt | awk -F. '{ print $1","$2","$3","$4}' > test.csv

but I am getting output in just 3 columns. I want every value to be printed in new column.

I have also used this one but result is not accurate.


Code:
awk -F_ '{ print $1","$2","$3}' < memory.txt >  MemoryTemp.csv

plz tell me where i am doing wrong. i got this code from google.

Thanks in advance.

Last edited by zaxxon; 07-30-2010 at 04:35 AM.. Reason: added 1 more pair of missing code tags
Sponsored Links
    #2  
Old 07-30-2010
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 

Join Date: Sep 2007
Location: Germany
Posts: 5,193
Thanks: 47
Thanked 280 Times in 267 Posts
Since the 1st awk code just handles only first 3 fields you only get first 3 fields. There is also a substitution of underscores where there aren't any. So a following underscore as field separator is not working either. This code does not fit to your question at all. You can try this one - I've chosen the semicolon as separator:


Code:
$> sed 's/[[:space:]]\+/;/g' memory.txt > memory.csv
$> cat memory.csv
Average:;822387;7346605;89.93;288845;4176593;2044589;51883;2.47;7600

Sponsored Links
    #3  
Old 07-30-2010
The Underground
 

Join Date: Sep 2009
Location: ./India/Mumbai
Posts: 889
Thanks: 28
Thanked 179 Times in 174 Posts
Hi,
Try this,

Code:
tr -s ' ' ',' < inputfile

    #4  
Old 07-30-2010
Registered User
 

Join Date: Feb 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

zaxxon: by choosing , (comma) as a separator, i got the required results. so thanks for your input.


Code:
sed 's/[[:space:]]\+/,/g' memory.txt > memory.csv

pravin27: your code is also working so thanks for your efforts.

Regards,
Kashif.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
converting fixed length file to delimited file Nishithinfy Shell Programming and Scripting 2 07-02-2009 07:50 AM
Problem in converting password protected excel file to csv file in unix Devivish UNIX for Advanced & Expert Users 2 03-30-2009 07:56 PM
Converting a Delimited File to Fixed width file raghavan.aero Shell Programming and Scripting 2 06-06-2007 02:44 PM
Converting ps file to pdf jasjot31 Shell Programming and Scripting 3 09-27-2006 06:09 AM



All times are GMT -4. The time now is 04:02 AM.