settle
settle

Reputation: 139

Deleting multiple columns in .csv file in unix

I have csv file with 80 columns and "^A" delimiter. I want to delete few columns from it, can be 1,2,20,31,45,56,77,78,79,80 or what ever column needed.

For Example: I am taking a CSV file with 15 columns From:

2018^A04 14:14:46^A01^AJHFM^A2^ACard^Aacc^A11^A0^AVZ^Aapp^A2^AGold^ACUST^ABB  
2018^A04 14:14:46^A01^AJHFM^A2^ACard^Aacc^A11^A0^AVZ^Aapp^A2^AGold^ACUST^ABB  
2018^A04 14:14:46^A01^AJHFM^A2^ACard^Aacc^A11^A0^AVZ^Aapp^A2^AGold^ACUST^ABB  
2018^A04 14:14:46^A01^AJHFM^A2^ACard^Aacc^A11^A0^AVZ^Aapp^A2^AGold^ACUST^ABB  
2018^A04 14:14:46^A01^AJHFM^A2^ACard^Aacc^A11^A0^AVZ^Aapp^A2^AGold^ACUST^ABB  
2018^A04 14:14:46^A01^AJHFM^A2^ACard^Aacc^A11^A0^AVZ^Aapp^A2^AGold^ACUST^ABB  
2018^A04 14:14:46^A01^AJHFM^A2^ACard^Aacc^A11^A0^AVZ^Aapp^A2^AGold^ACUST^ABB  
2018^A04 14:14:46^A01^AJHFM^A2^ACard^Aacc^A11^A0^AVZ^Aapp^A2^AGold^ACUST^ABB  
2018^A04 14:14:46^A01^AJHFM^A2^ACard^Aacc^A11^A0^AVZ^Aapp^A2^AGold^ACUST^ABB  
2018^A04 14:14:46^A01^AJHFM^A2^ACard^Aacc^A11^A0^AVZ^Aapp^A2^AGold^ACUST^ABB  

To:

I deleted columns 2,5,7,9,13

2018^A01^AJHFM^ACard^A11^AVZ^Aapp^A2^ACUST^ABB  
2018^A01^AJHFM^ACard^A11^AVZ^Aapp^A2^ACUST^ABB  
2018^A01^AJHFM^ACard^A11^AVZ^Aapp^A2^ACUST^ABB  
2018^A01^AJHFM^ACard^A11^AVZ^Aapp^A2^ACUST^ABB  
2018^A01^AJHFM^ACard^A11^AVZ^Aapp^A2^ACUST^ABB  
2018^A01^AJHFM^ACard^A11^AVZ^Aapp^A2^ACUST^ABB  
2018^A01^AJHFM^ACard^A11^AVZ^Aapp^A2^ACUST^ABB  
2018^A01^AJHFM^ACard^A11^AVZ^Aapp^A2^ACUST^ABB  
2018^A01^AJHFM^ACard^A11^AVZ^Aapp^A2^ACUST^ABB  
2018^A01^AJHFM^ACard^A11^AVZ^Aapp^A2^ACUST^ABB  

It worked using awk but it's giving empty spaces in the deleted columns.

awk 'BEGIN { FS="^A"} {$1=$2="";gsub(",+",",",$0)}1' filename

Upvotes: 2

Views: 97

Answers (1)

glenn jackman
glenn jackman

Reputation: 246807

when you assign to a field, awk will rewrite the line using the output field separator. You need to set that, as by default it is a space. Do this:

BEGIN { FS = OFS = "^A" }

Upvotes: 1

Related Questions