在UNIX中的.csv文件中删除多列

时间:2018-08-02 21:13:12

标签: shell

我有80列和“ ^ A”定界符的csv文件。我想从中删除几列,可以是1,2,20,31,45,56,77,78,79,80或需要的任何列。

例如:我正在获取一个15列的CSV文件 发件人:

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  

收件人:

我删除了列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  

它使用awk可以工作,但是在已删除的列中留有空白。

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

1 个答案:

答案 0 :(得分:1)

分配给字段时,awk将使用 output 字段分隔符重写该行。您需要设置它,因为默认情况下它是一个空格。这样做:

BEGIN { FS = OFS = "^A" }