“Usando sed para encontrar e substituir string complexa (de preferência por regex)” Respostas de código

Usando sed para encontrar e substituir string complexa (de preferência por regex)

sed -i -E "s/(<username>.+)name(.+<\/username>)/\1something\2/" file.xml
DreamCoder

Usando sed para encontrar e substituir string complexa (de preferência por regex)

sed -e '/username/s/CDATA\[name\]/CDATA\[something\]/' \
-e '/password/s/CDATA\[password\]/CDATA\[somethingelse\]/' \
-e '/dbname/s/CDATA\[name\]/CDATA\[somethingdifferent\]/' file.txt
DreamCoder

Usando sed para encontrar e substituir string complexa (de preferência por regex)

$ sed -e '1s/name/something/2' \
      -e '3s/name/somethingdifferent/2' \
      -e 's/password/somethingelse/2' sample.xml
DreamCoder

Usando sed para encontrar e substituir string complexa (de preferência por regex)

sed -i -E "s/(<username>.+)name(.+<\/username>)/\1something\2/" file.xml
This is, I think, what you're looking for.

Explanation:

parentheses in the first part define groups (strings in fact) that can be reused in the second part
\1, \2, etc. in the second part are references to the i-th group captured in the first part (the numbering starts with 1)
-E enables extended regular expressions (needed for + and grouping).
-i enables "in-place" file edit mode
DreamCoder

Respostas semelhantes a “Usando sed para encontrar e substituir string complexa (de preferência por regex)”

Perguntas semelhantes a “Usando sed para encontrar e substituir string complexa (de preferência por regex)”

Mais respostas relacionadas para “Usando sed para encontrar e substituir string complexa (de preferência por regex)” em Shell/Bash

Procure respostas de código populares por idioma

Procurar outros idiomas de código