次のファイルに値があります。
"AERH1","505549_AdelaideCBDWest_3@505549_AdelaideCBDWest_3",BTS3900,16,16,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
"AERH1","558018_ElizabethVale_3@558018_ElizabethVale_3_UL",BTS3900,448,336,6,1,1,90,0,1,1,1,1,1,0,6,0,6,6,6,6,0,6,0,0,0,0,0,6,6,0,0,0,6,6,0,6,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,15,0,0,0,0,0,0,1,0,0,0,0,0,0,6,0,6,0,6,0,0,0,0,0,0,0,0,0,0,0,0,
"AERH1","505138_Netley_3@505138_Netley_3",BTS3900,448,336,6,1,1,90,0,1,1,1,1,1,0,6,0,6,6,6,6,0,6,0,0,0,0,0,6,6,0,0,0,6,6,6,6,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,12,0,1,0,0,0,0,1,0,1,0,0,0,0,6,0,6,0,6,0,0,0,0,0,0,0,0,0,6,0,0,
"AERH1","558012_OneTreeHill_2@558012_OneTreeHill_2_UL",BTS3900,544,400,9,1,1,135,0,1,1,1,1,1,1,9,0,9,9,9,9,0,9,0,0,0,0,0,9,9,0,0,0,9,9,12,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,30,0,1,0,0,0,0,1,0,3,0,0,0,0,9,0,9,0,9,0,0,0,0,0,0,0,0,0,0,0,0,
以下のように、各行に最大6つのカンマを選択する必要があります。
"AERH1","505549_AdelaideCBDWest_3@505549_AdelaideCBDWest_3",BTS3900,16,16,1
答え1
使用cut
:
cut -d, -f 1-6 file
またはawk
:
awk -F, '{OFS=","; print $1,$2,$3,$4,$5,$6}' file
どちらの場合も、出力は次のようになります。
"AERH1","505549_AdelaideCBDWest_3@505549_AdelaideCBDWest_3",BTS3900,16,16,1
"AERH1","558018_ElizabethVale_3@558018_ElizabethVale_3_UL",BTS3900,448,336,6
"AERH1","505138_Netley_3@505138_Netley_3",BTS3900,448,336,6
"AERH1","558012_OneTreeHill_2@558012_OneTreeHill_2_UL",BTS3900,544,400,9
答え2
1つのオプションはawkを使用することです。
awk -F, '{ for(i=1;i<=5;i++) { printf "%s,",$i } printf "%s\n",$6 }' filename
カンマで区切られた各フィールドを最大5回参照し、各フィールドの後にカンマを入力して最後に6番目のセクションを印刷します。
答え3
CSVにはいくつかの不快なケースがあるため、CSVパーサーを使用することをお勧めします。たとえば、Rubyの場合:
ruby -rcsv -e 'CSV.foreach(ARGV.shift) {|row| puts CSV.generate_line(row.first(6))}' file