How to delete column from file?


#1

Hi,

I have a comma seperated file and would like to remove an empty column from this file.

For example, the file could be:

a,,b,c
a,,b,c
a,,b,c

This means that the second column is empty. The resulting file then should be

a,b,c
a,b,c
a,b,c

This should work for more than one empty column. Does anyone know how to remove such blank column or columns from a comma separated file:confused:


#2

Hi,

If this is a file with fixed set of columns and you know what column you wish to remove simply use cut command:

$ cat csv.txt
a,,b,c
a,,b,c
a,,b,c
$ cut -d, -f1,3,4 csv.txt
a,b,c
a,b,c
a,b,c

In case that you do not have a fixed columns and also do not know what column is empty you can try use the following script or simply get it from an attachment. Copy it locally and make it executable:

$ chmod +x column.pl

To use it input two arguments: input file and output file

$ cat csv.txt
a,,b,c
a,,b,c
a,,b,c
$ ./column.pl csv.txt new_csv.txt
$ cat new_csv.txt
a,b,c
a,b,c
a,b,c

Hope this helps

Lubos


#3