1

I've got the following paragraph (saved on a file text.txt):

Good morning, my name is Maria.
I study in the university. 
I like to travel.

And I would like to end up with three paragraphs exactly like that one but with different female names. Desired output:

Good morning, my name is Maria.
I study in the university. 
I like to travel.

Good morning, my name is Lola.
I study in the university. 
I like to travel.

Good morning, my name is Veronica.
I study in the university. 
I like to travel.

So I made a txt file with the names, names.txt:

Maria
Lola
Veronica

And I wanted to come up with a for loop that iterates the paragraph but replaces the names with sed. So I did:

for i in names.txt; do sed 's/Maria/"$i"/g' text.txt; done

But what I get is this:

Good morning, my name is "$i".
I study in the university. 
I like to travel.

Someone knows what could be wrong in my code? Thank you!

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
msimmer92
  • 397
  • 3
  • 16

1 Answers1

1

Use :

for i in $(< names.txt); do
    sed "s/Maria\./$i\n/g" text.txt
    echo # newline
done

Quoting issue and your forgot to read the file. That's what I do with

$(< names.txt)

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223