Home > Blockchain >  Copy all the text in "File1" and replace it with a specific text in File 2 using sed comma
Copy all the text in "File1" and replace it with a specific text in File 2 using sed comma

Time:11-18

Let's say I have a file called File1.txt that has the string

Hamburger

And I have another file called File2.txt that has the string:

I love Pizza

I want to use the sed command to make changes such that it copies all the text from File1.txt i.e. Hamburger and replace it in File2.txt with the word Pizza so that the final output in File2.txt would be

I love Hamburger

Is there a way to do this suing the sed command ?

Here's an example of code I am trying to use but it doesn't work:

sed -e '/Hamburger/{r File1.txt' -e 'd}' File2.txt

CodePudding user response:

You can try this sed

$ sed "s/Pizza/$(cat File1.txt)/" File2.txt
I love Hamburger

CodePudding user response:

The question is tagged github-actions, so I am going to make some guesses.

  • You have a config file with some template data in it.
  • You want to automatically replace that with some real data that is stored in another file.

There are many ways you could do this, but here's one using envsubst

First, rewrite your template File2.txt this way

I love $Pizza

Then run this shell script:

export Pizza=$(<File1.txt)
envsubst '$Pizza' < File2.txt

This will print out the phrase you expect by expanding $Pizza within the file to the content of the corresponding environment variable, but not expanding any other things that look like environment variables.

  • Related