Como mover uma linha para a linha anterior se um determinado caractere “:” não for encontrado? [duplicado]

0

Eu tenho muitos arquivos de texto de transcrições. Eu limpei isso até certo ponto. A última parte da limpeza é a seguinte.

Eu tenho isso em certos arquivos * .txt

Gary: I said something.
Larry: I said something else.
Mr. John: I said this. And maybe this
and I also said this.
Laura: did i say anything.

Eu preciso disso assim.

Gary: I said something.
Larry: I said something else.
Mr. John: I said this. And maybe this and I also said this.
Laura: did i say anything.

Eu quero mover qualquer linha não contendo dois pontos (:) para a linha anterior. No final, quero que cada linha tenha um diálogo de personagem que termine com uma nova linha.

Eu olhei para essa pergunta mas não consegui entender o que fazer. Estou aberto a qualquer ferramenta sed / awk / python / bash / perl.

    
por scientific_explorer 12.10.2018 / 14:20

4 respostas

0

Com Sed, você poderia anexar uma linha ao espaço de padrão, verificar se a parte anexada (da nova linha adicionada ao final do padrão) contém apenas caracteres não-cônicos e, em caso afirmativo, substituir a última nova por um espaço:

sed -e :a -e '$!N; s/\n\([^:]*\)$/ /;ta' -e 'P;D' file.txt
Gary: I said something.
Larry: I said something else.
Mr. John: I said this. And maybe this and I also said this.
Laura: did i say anything.
    
por 12.10.2018 / 14:39
0

Que tal awk ? Mantém uma cópia da última linha; se nenhum cólon encontrado (NF == 1) anexar a linha atual à última para imprimir ambas de uma só vez. $ 0 está definido para a string vazia, por isso não será lembrado.

awk -F: 'NF == 1 {LAST = LAST " " $0; $0 = ""}; LAST {print LAST}; {LAST = $0} END {print LAST}' file
Gary: I said something.
Larry: I said something else.
Mr. John: I said this. And maybe this and I also said this.
Laura: did i say anything.
    
por 12.10.2018 / 15:14
0

Outra awk tentativa:

BEGIN{RS=":";ORS=":"; # use ":", ie. change of speaker, to recognise end of record
      FS="\n"}        # OFS is still " ", so newlines in input converted to spaces in output
!$NF { ORS="" }       # detect last line (no next speaker) and don't append a :
NF>1 {$NF = "\n" $NF} # restore the newline before the speaker's name
{print}               # print the result
    
por 12.10.2018 / 15:38
0
sed -e '
   /:/{$!N;}
   /\n.*:/!s/\n/ /
   P;D
' file.txt

 Gary: I said something.
 Larry: I said something else.
 Mr. John: I said this. And maybe this and I also said this.
 Laura: did i say anything.
    
por 12.10.2018 / 18:43