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