Como escolher um valor até um valor específico alcançado em uma linha

0

Eu tenho valores em um arquivo como este

"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,

Eu preciso selecionar até 6 vírgulas em cada linha como esta

"AERH1","505549_AdelaideCBDWest_3@505549_AdelaideCBDWest_3",BTS3900,16,16,1

Por favor, me sugira a função no shell script.

    
por Meenakshisundaram 05.10.2017 / 09:53

3 respostas

1

Use cut :

cut -d, -f 1-6 file

Ou awk :

awk -F, '{OFS=","; print $1,$2,$3,$4,$5,$6}' file

Em ambos os casos, a saída será:

"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
    
por 05.10.2017 / 11:09
0

Uma opção é usar o awk:

awk -F, '{ for(i=1;i<=5;i++) { printf "%s,",$i } printf "%s\n",$6 }' filename

Procure em cada campo separado por vírgula até cinco, imprimindo cada campo seguido por uma vírgula e, finalmente, imprima a 6ª parte.

    
por 05.10.2017 / 10:19
0

O CSV tem alguns casos desagradáveis, então é melhor usar um analisador de CSV. Por exemplo, com Ruby:

ruby -rcsv -e 'CSV.foreach(ARGV.shift) {|row| puts CSV.generate_line(row.first(6))}' file
    
por 05.10.2017 / 16:17