Saída de pré-formatação CSV

4

Eu tenho um script bash que converte este arquivo "origin.txt"

cxx-yyy-zzz-999-111
2018-01-1T00:10:54.412Z
2018-01-5T00:01:19.447Z
1111-6b54-eeee-rrrr-tttt
2018-01-1T00:41:38.867Z
2018-01-5T01:14:55.744Z
1234456-1233-6666-mmmm-12123
2018-01-1T00:12:37.152Z
2018-01-5T00:12:44.307Z

para

cxx-yyy-zzz-999-111,2018-01-1T00:10:54.412Z,2018-01-5T00:01:19.447Z
1111-6b54-eeee-rrrr-tttt,2018-01-1T00:41:38.867Z,2018-01-5T01:14:55.744Z
1234456-1233-6666-mmmm-12123,2018-01-1T00:12:37.152Z,2018-01-5T00:12:44.307Z

Como eu poderia fazer isso com o AWK?

    
por kdetony 06.08.2018 / 17:15

2 respostas

4

Eu tenho um arquivo de script awk chamado tmp.awk com o seguinte conteúdo

BEGIN {
   i=0
}
{
   if (i==0) {
      f1=$0
      i++
   } else if (i==1) {
      f2=$0
      i++
   } else {
      i=0
      print f1","f2","$0
   }
}

E um arquivo origin.txt com o seguinte conteúdo

cxx-yyy-zzz-999-111
2018-01-1T00:10:54.412Z
2018-01-5T00:01:19.447Z
1111-6b54-eeee-rrrr-tttt
2018-01-1T00:41:38.867Z
2018-01-5T01:14:55.744Z
1234456-1233-6666-mmmm-12123
2018-01-1T00:12:37.152Z
2018-01-5T00:12:44.307Z

Aqui está o meu comando awk e exemplo de saída.

zb@server ~ $ awk -f tmp.awk origin.txt 
cxx-yyy-zzz-999-111,2018-01-1T00:10:54.412Z,2018-01-5T00:01:19.447Z
1111-6b54-eeee-rrrr-tttt,2018-01-1T00:41:38.867Z,2018-01-5T01:14:55.744Z
1234456-1233-6666-mmmm-12123,2018-01-1T00:12:37.152Z,2018-01-5T00:12:44.307Z
    
por 06.08.2018 / 18:37
4

Usando sed:

sed 'N;N;s/\n/,/g' origin.txt 

Usando o awk:

awk '{ printf "%s", $0; if (NR % 3 == 0) print ""; else printf "," }'  origin.txt 

Saída:

cxx-yyy-zzz-999-111 2018-01-1T00:10:54.412Z 2018-01-5T00:01:19.447Z
1111-6b54-eeee-rrrr-tttt 2018-01-1T00:41:38.867Z 2018-01-5T01:14:55.744Z
1234456-1233-6666-mmmm-12123 2018-01-1T00:12:37.152Z 2018-01-5T00:12:44.307Z
    
por 06.08.2018 / 19:04