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:

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:

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

how do i do this? Please suggest

You can use sed or tr. The ff. links may prove to be useful:

http://www.unix.com/shell-programming-scripting/19160-how-remove-spaces-string-using-sed.html

http://www.unix.com/shell-programming-scripting/44918-sed-remove-whitespace.html

http://www.unix.com/shell-programming-scripting/98470-replace-space-spaces-line-file-single.html

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

Try...

awk 'BEGIN {
        FS = "\t"
        OFS = ","
     }

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

Ok, I get it. Sorry man. But I think Ygor got it :slight_smile:

Hi,
Thanks a lot.. this works for me :slight_smile: