Adicione colchetes para latitude e longitude

0

Eu tenho um arquivo CSV que tem longitudes e latitudes:

input.csv

103.771 1.44849,103.771 1.44894,103.771 1.4489,103.772 1.44802,103.772 1.4479,103.774 1.44948,103.775 1.4501,103.778 1.4512,103.78 1.45145,103.781 1.45151,103.783 1.45118,103.786 1.45035,103.787 1.45007,103.789 1.4492,103.79 1.44898,103.791 1.44918,103.793 1.4498,103.793 1.44984,103.794 1.44939,103.795 1.44816,103.797 1.44214,103.798 1.44095,103.797 1.44036,103.796 1.43938,103.793 1.4383,103.793 1.43779,103.791 1.43624,103.79 1.43503,103.789 1.43341,103.788 1.43235,103.788 1.43208,103.788 1.43161,103.787 1.4299,103.787 1.42869,103.788 1.42771,103.789 1.42693,103.79 1.42592,103.79 1.42401,103.79 1.42279,103.79 1.42149,103.789 1.41974,103.789 1.41716,103.788 1.4146,103.788 1.41244,103.787 1.41201,103.786 1.41175,103.785 1.41141,103.784 1.41095,103.783 1.41079,103.782 1.41072,103.78 1.41074,103.78 1.41074,103.778 1.41071,103.776 1.41079,103.775 1.41056,103.773 1.41008,103.772 1.4101,103.771 1.42498,103.772 1.43239,103.772 1.43484,103.772 1.43566,103.771 1.44849

Eu quero adicionar colchetes e uma vírgula entre cada conjunto de coordenadas que resultará como

output.csv

[103.771,1.44849],[103.771,1.44894],[103.771,1.4489],[103.772,1.44802],[103.772,1.4479],[103.774,1.44948],[103.775,1.4501],[103.778,1.4512],[103.78,1.45145],[103.781,1.45151],[103.783,1.45118],[103.786,1.45035],[103.787,1.45007],[103.789,1.4492],[103.79,1.44898],[103.791,1.44918],[103.793,1.4498],[103.793,1.44984],[103.794,1.44939],[103.795,1.44816],[103.797,1.44214],[103.798,1.44095],[103.797,1.44036],[103.796,1.43938],[103.793,1.4383],[103.793,1.43779],[103.791,1.43624],[103.79,1.43503],[103.789,1.43341],[103.788,1.43235],[103.788,1.43208],[103.788,1.43161],[103.787,1.4299],[103.787,1.42869],[103.788,1.42771],[103.789,1.42693],[103.79,1.42592],[103.79,1.42401],[103.79,1.42279],[103.79,1.42149],[103.789,1.41974],[103.789,1.41716],[103.788,1.4146],[103.788,1.41244],[103.787,1.41201],[103.786,1.41175],[103.785,1.41141],[103.784,1.41095],[103.783,1.41079],[103.782,1.41072],[103.78,1.41074],[103.78,1.41074],[103.778,1.41071],[103.776,1.41079],[103.775,1.41056],[103.773,1.41008],[103.772,1.4101],[103.771,1.42498],[103.772,1.43239],[103.772,1.43484],[103.772,1.43566],[103.771,1.44849]

Como posso fazer isso no Linux?

    
por priyanka3 17.11.2016 / 02:52

3 respostas

0
sed "s#,#],[#g;s# #,#g;s#^#[#;s,$,]," input > output

solução awk

awk -F, '{
for(i=1;i<=NF;i++)
{
     sub(" ",",",$i);
     i==NF?out=O $i C:out=O $i C",";
     printf("%s",out)
}
}'  O="[" C="]" input > output
awk '{gsub(",","],["); sub(/^/,"["); sub(/$/,"]"); sub(" ",",")}1' input > output
    
por 17.11.2016 / 04:01
0

Eu poderia tentar:

 cat infile | sed 's/,/\],\[/g
 s/ /,/g
 s/^/\[/
 s/$/\]/' > outfile
    
por 17.11.2016 / 03:45
0

Como você marcou sua pergunta awk , uma maneira de fazer isso (sem passar pelos campos explicitamente) seria

awk -F, -vOFS='],[' '
  {$0 = "[" $0 "]"; $1=$1; gsub(" ",","); print}
' input.csv

No entanto IMHO seria mais simples e natural simplesmente combinar e capturar pares de strings separados por espaços e substituí-los novamente em sed

sed -E 's/([^,]*) ([^,]*)/\[,\]/g' input.csv
    
por 17.11.2016 / 08:08