Replace part of text with Script [duplicate]

1

I'm starting to program and I have a question, I have a lot of .txt to which I have put the same extension in a line, I want to change that word in each of the .txt for those in a list, I do not know very well as I go through the list so that I can pick up the lines, I would like to do it in bash, I do not know if you can help me.

I've tried something like this:

for i in *txt
sed -i "s/texto_a_reemplazar/$lista/" *.txt
done

Thanks in advance

Greetings,

    
asked by Roxalto 09.11.2018 в 08:52
source

2 answers

0

I did not really understand the part of the list that is going to replace the chain. But I will try to leave in general a process that I hope will help.

#!/bin/bash

ruta="."
separador=","
texto_a_reemplazar="texto_a_reemplazar"

declare -a lista
lista=(
    texto1
    texto2
    texto3
    )
#Esto es lo que yo entiendo por "lista"

lista_a_cadena="$(
    IFS="$separador"
    printf "${lista[*]}"
)"
# ^^^^ esto te crea la variable lista_a_cadena como una cadena de la forma "texto1,texto2,texto3"
#      puedes cambiar el separador cambiando el valor del IFS

sed "s/$texto_a_reemplazar/$lista_a_cadena/g" $ruta/*.txt
#               El bucle "for" no es  necesario    ^^^^
#               cuando podrías hacer un pathname expansion

The result obtained is something of this style.

$ ./reemplazar
texto1,texto2,texto3:texto fijo
texto1,texto2,texto3:texto fijo
texto1,texto2,texto3:texto fijo

Since I had three files that contained the string texto_a_reemplazar:texto fijo

To test if the changes are the way you want them, remove the parameter -i from sed , just like I did.

In the link that @fedorqui shared more examples, and well explained, how to replace text in several files.

If you want information about the expansion pathname (and other expansions) you can consult the bash man 1 bash manual in the EXPANSION section

Reading the manuals is going to be very useful if you are learning to program scripts in bash.

To find the part of expansions you can use a regular expression in your pager of the manual (it is common that it is the program less ). For this you will need to put this when the manual opens.

/^EXPANSION$

And the pager will take you to that coincidence.

Update 1

Assuming that your hostname.txt file has a text with the following format:

host1
host2
host3

And you want to replace that list in files that have this structure:

palabra1:palabra_a_sustituir:palabra3

I can think of the following.

#!/bin/bash

carpeta="./archivos"  # En esta carpeta están tus archivos.
separador=","

declare -a lista
lista=($(cat hostname.txt))

sed "s/palabra_a_sustituir/$(echo ${lista[@]})/g" $carpeta/*  # Versión 1
sed "s/palabra_a_sustituir/$(IFS="$separador"; echo "${lista[*]}")/g" archivos/*  # Versión 2

Version 1 will throw you an output of this type for each match in the files in the folder.

palabra1:host1 host2 host3:palabra3

Version 2 is going to throw you a way out for every match.

palabra1:host1,host2,host3:palabra3
    
answered by 10.11.2018 в 05:52
0

Thanks for the answers, I have been testing this solution to my case and I comment things:

  • I have the list in a .txt file called "hostname.txt"

  • My intention is to make it simple by substituting a word that is in all the files, by those that are in the list. I tried to do it this way:

    lista=$(cat hostname.txt)
    
    sed "/palabra_a_sustituir/$lista/g *.cfg
    

I do not know if I'm missing anything else, the truth is that I'm reading but I do not know if I'm worth it for this case.

Thank you very much for your answers.

    
answered by 14.11.2018 в 11:58