Shell script for converting file contents into CSV


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script for converting file contents into CSV
# 1  
Old 02-06-2009
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  
Old 02-06-2009
# 3  
Old 02-06-2009
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  
Old 02-06-2009
Try...
Code:
awk 'BEGIN {
        FS = "\t"
        OFS = ","
     }

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

# 5  
Old 02-06-2009
Quote:
Originally Posted by rish_max
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 Smilie
# 6  
Old 02-06-2009
MySQL

Hi,
Thanks a lot.. this works for me Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell Script to Read the given file contents into a merged one file

Like to have shell script to Read the given file contents into a merged one file with header of path+file name followed by file contents into a single output file. While reading and merging the file contents into a single file, Like to keep the format of the source file. ... (4 Replies)
Discussion started by: Siva SQL
4 Replies

2. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

3. Shell Programming and Scripting

Help with Shell Scrip in Masking particular columns in .csv file or .txt file using shell script

Hello Unix Shell Script Experts, I have a script that would mask the columns in .csv file or .txt file. First the script will untar the .zip files from Archive folder and processes into work folder and finally pushes the masked .csv files into Feed folder. Two parameters are passed ... (5 Replies)
Discussion started by: Mahesh G
5 Replies

4. Shell Programming and Scripting

Modifying contents of the file in shell script

Hello all, I have a Kconfig file that looks like something below ... ================================ menu "Application type" config GUI_TYPE_STANDARD bool "Standard Application" source "cfg/config/std.in" source... (12 Replies)
Discussion started by: anand.shah
12 Replies

5. Shell Programming and Scripting

Shell Script for converting file to Excel or CSV

Hi I have a dat file which has "n" number of columns. The file is delimited. The number of columns keep varying as the file is generated out of DB queries. Could you please help me in writing a script which will generate a XLS or CSV file out of the dat file. (5 Replies)
Discussion started by: Vee
5 Replies

6. Shell Programming and Scripting

Converting DOS Batch file to Shell Script

Hi, This is my DOS Batch file. @echo off echo "Program Name :" %0 rem echo "Next param :" %1 echo "Next param :" "Username/Password" echo "User Id :" %2 echo "User Name :" %3 echo "Request ID ... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

7. Shell Programming and Scripting

shell script to compare file contents

Hello Has anyone got an example shell script that I can use to compare the contents of two files. The files should contain the same contents, eg. file1.txt apple pear grape file2.txt apple pear grape (2 Replies)
Discussion started by: deedaz
2 Replies

8. 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

9. Shell Programming and Scripting

update file contents using shell script

Hi, I am having a file which contains as below Names(aaaa ,bbbb ,cccc ,dddd) now i want the file to be updated with new value 'eeee' as below Names(aaaa ,bbbb ,cccc ,dddd ,eeee) Is there a way to script this ? Thanks, (5 Replies)
Discussion started by: drams
5 Replies

10. Shell Programming and Scripting

search for the contents in many file and print that file using shell script

hello have a file1 H87I Y788O T347U J23U and file2 J23U U887Y I99U T556U file3 I99O J99T F557J file4 N99I T666U R55Y file5 H87I T347U file6 H77U R556Y E44T file7 Y788O K98U H8I May be using script we can use file1 to search for all the files and have the output H87I file5... (3 Replies)
Discussion started by: cdfd123
3 Replies
Login or Register to Ask a Question