The UNIX and Linux Forums  


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
update file contents using shell script drams Shell Programming and Scripting 5 10-09-2008 04:56 PM
Unix shell script to parse the contents of comma-separated file KrishnaSaran Shell Programming and Scripting 11 06-20-2008 06:43 AM
Converting Shell Script to HTML davwel Shell Programming and Scripting 3 10-25-2007 11:25 PM
search for the contents in many file and print that file using shell script cdfd123 Shell Programming and Scripting 3 10-07-2007 11:17 PM
converting contents of a character array to int jyotipg High Level Programming 5 07-19-2006 10:18 PM

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

Join Date: Feb 2009
Posts: 9
Shell script for converting file contents into CSV

Hi, I am new in unix,
I just want to replace some values from text file according to column numbers. Like, I am having a table as given below:


Code:
val1     val2     val3     val4        val5
val6     val7     val8                   val9
val10    val11              val12      val13

Now i want to replcae the blank value for a column with 'null' and then converted it into comma separated value(csv) as:


Code:
val1,val2,val3,val4,val5
val6,val7,val8,null,val9
val10,val11,null,val12,val13

how do i do this? Please suggest

Last edited by Ygor; 02-06-2009 at 02:19 AM.. Reason: Added CODE tags for readability.
  #2 (permalink)  
Old 02-06-2009
angheloko's Avatar
angheloko angheloko is offline
Registered User
  
 

Join Date: Jul 2008
Location: Philippines
Posts: 125
You can use sed or tr. The ff. links may prove to be useful:

how to remove spaces in a string using sed.

sed : remove whitespace

replace space or spaces in a line of a file with a single :
  #3 (permalink)  
Old 02-06-2009
rish_max rish_max is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 9
Thanx for quick reply, but it doesn't work for me.
I rephrase my problem:
Every row in a file should have 5 seperate columns in which values are tab seperated.
and there are some blank values, say column 4 of row 2 is blank and column 3 of row 3 is blank. I need to first fill this blank value with 'null' and then convert it into CSV
i.e the script should consider the tab value as blank and replace it with ',' but should not consider the blank value of a column as blank space.
sed will replace tabs and blank values of a column with ',' so the output becomes
row 1 has all 5 values seperated with comma
row 2 has first 3 values seperated with comma and then sed consider the tab space and blank value as blankspace and replace it with comma and then write the 5th column value so the output becomes val1,val2,val3,val5. It should be like val1,val2,val3,null,val5
  #4 (permalink)  
Old 02-06-2009
angheloko's Avatar
angheloko angheloko is offline
Registered User
  
 

Join Date: Jul 2008
Location: Philippines
Posts: 125
Quote:
Originally Posted by rish_max View Post
Thanx for quick reply, but it doesn't work for me.
I rephrase my problem:
Every row in a file should have 5 seperate columns in which values are tab seperated.
and there are some blank values, say column 4 of row 2 is blank and column 3 of row 3 is blank. I need to first fill this blank value with 'null' and then convert it into CSV
i.e the script should consider the tab value as blank and replace it with ',' but should not consider the blank value of a column as blank space.
sed will replace tabs and blank values of a column with ',' so the output becomes
row 1 has all 5 values seperated with comma
row 2 has first 3 values seperated with comma and then sed consider the tab space and blank value as blankspace and replace it with comma and then write the 5th column value so the output becomes val1,val2,val3,val5. It should be like val1,val2,val3,null,val5
Ok, I get it. Sorry man. But I think Ygor got it
  #5 (permalink)  
Old 02-06-2009
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
Try...
Code:
awk 'BEGIN {
        FS = "\t"
        OFS = ","
     }

     {
        $1 = $1
        for (i = 1; i <= NF; i++) {
           if ($i == "") {
              $i = "null"
           }
        }
        print $0
     }' file1 > file2

  #6 (permalink)  
Old 02-06-2009
rish_max rish_max is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 9
Thumbs up

Hi,
Thanks a lot.. this works for me
Closed Thread

Bookmarks

Tags
file

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 12:53 PM.


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