CSVは新しい列を作成し、スペースを削除します。

CSVは新しい列を作成し、スペースを削除します。

複数の列を含む大きなCSVファイルがあります。最初の列のスペースを削除して新しい列にしたいと思います。

はい...

入力する:

a b,xyz,d e f    
a b c,xyz,d e f    
a b c d,xyz,d e f   

出力:

ab,a b,xyz,d e f   
abc,a b c,xyz,d e f   
abcd,a b c d,xyz,d e f    

ベストアンサー1

使用sed:

sed 'h;s/,.*/,/;s/ //g;G;s/\n//' data.csv

説明する:

h - stash current line to the hold space
s/,.*/,/ - remove everything after the first comma
s/ //g - remove spaces
G - append the line from the hold space back to the pattern space
s/\n// - remove extra newline, as left by G

おすすめ記事