Pular para o conteúdo

Comando SED - Alterar segunda ocorrência no texto e não na linha, é possível? [RESOLVIDO]

Responder tópico
  • Denunciar
  • Indicar

1. Comando SED - Alterar segunda ocorrência no texto e não na linha, é possível? [RESOLVIDO]

Enviado em 12/03/2019 - 12:05h

Olá pessoal,

Tenho um shell script que vai alterar e inserir valores em um texto pronto.

Texto:

<h2> Teste </h2>
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
<h2> Teste </h2>
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
<h2> Teste </h2>
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
<h2>Teste </h2>
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
Eu preciso alterar o segundo ou terceiro, as vezes o quarto <h2> que aparece no texto. Achei como alterar a primeira e a última ocorrência no texto, mas não uma determinada.

Na linha usamos sed 's/<h2>/Teste<h2>/2' texto.txt onde o 2 aponta a segunda ocorrência em todas as linhas.

Os textos variam os números de linha, sendo assim não consigo pegar determinada linha.

A minha dúvida é, tem como alterar a segunda ou terceira ocorrência no texto?

Fico grato pela ajuda.

Responder tópico

2. Re: Comando SED - Alterar segunda ocorrência no texto e não na linha, é possível?

Melhor resposta

Enviado em 12/03/2019 - 13:59h

Usando awk:
awk '/<h2>/{c++;if(c==2){sub("Teste","OUTROTEXTO")}}1' arquivo.txt 

Onde "c==2" é número da ocorrência, no caso vai alterar "Teste" por "OUTROTEXTO" na segunda ocorrência de "<h2>".

Para salvar a alteração no mesmo arquivo use a opção "-i inplace" do "awk".

3. Re: Comando SED - Alterar segunda ocorrência no texto e não na linha, é possível? [RESOLVIDO]

Enviado em 12/03/2019 - 15:03h

Valeu mestre, funcionou!

Não tenho conhecimento em awk, vou estudar mais esse comando também =D

4. Re: Comando SED - Alterar segunda ocorrência no texto e não na linha, é possível? [RESOLVIDO]

Enviado em 13/03/2019 - 18:26h

Boa Noite.
Sei que está resolvido, mas,
segue uma sugestão, a qual mostra as "linhas" com o "padrão definido.

patt="<h2> Teste <\/h2>"
arq="texto.txt"
awk 'BEGIN{printf "\nAlterar o Texto da Linha:\n\n"}/'"${patt}"'/ {printf "[%02d] %s\n" ,NR,$0}END{printf "\n"}' ${arq} ;read -p "Linha: ";printf "\n";sed -r "${REPLY}s/[[:alpha:]]{2,}/#OUTRO_TEXTO#/" ${arq}

Att.: Marcelo Oliver

Responder tópico

Responder tópico

Entre na sua conta para responder.

Fazer login para responder