sed command to substitute contents from external file

Hi All,

I am trying to substitute a line in my existing file with the set of lines from another file. Regarding the existing file, i am sure that the line i am substituting will occur only at one place.

I have stored the text to be replaced in another file.

I am using below command.

cat InputFile.txt|sed "s/Parameters \"CParameters\"/`cat TextToReplace.txt`/"

I am using cat command to get the contents from second file.

But i am getting below error

sed: 0602-404 Function s/Parameters "CParameters"/      DSSUBRECORD cannot be parsed.

Here DSSUBRECORD is the first line from my TextToReplace.txt.

Please guide me on this error. Also please suggest on other ways to achieve this substitution requirement.

sed -e '/Parameters "CParameters"/r TextToReplace.txt' -e "//d" InputFile.txt
1 Like

Something like this:

File 1:

$ cat f1
abc
def
ghi

Replacement content file:

$ cat f2
rep1
rep2

To replace the pattern 'def' in f1 with the file content of f2:

$ sed -e '/def/r f2' -e '/def/d' f1
abc
rep1
rep2
ghi

Guru.

1 Like

First off sorry to the OP.I do not intend to hijack this thread but I only have a similar problemSo thought of asking it here than opening a new thread.

I have the following in a sh script called var

#!/bin/bash
var1=0

And on another script called main I use a if construct:

#!/bin/bash
. var
if [ "$var1" == "0"]
Do this
else
do that
fi

Now in do this part,I have to change the value of var1 from "0" to "1".Any help is much appreciated :).Thanks and once again sorry to OP.
Regards,
vijai

I tried this command, it is not giving any error but it is not able to find the pattern

   Parameters "CParameters"

and hence not replacing it with contents from TextToReplace.txt .

I tried to escape the double quotes which is part of my input pattern using \ , but no luck.

Seems to work here, and I'm pretty sure those are standard sed commands.

$ cat InputFile.txt
before
Parameters "CParameters"
after

$ cat TextToReplace.txt
aaa
bbb

$ sed -e '/Parameters "CParameters"/r TextToReplace.txt' -e "//d" InputFile.txt
before
aaa
bbb
after

$ sed -e '/CParameters/r TextToReplace.txt' -e "//d" InputFile.txt
before
aaa
bbb
after

You don't need to \ escape the "CParameters" double quotes, because inside the single quotes. I avoid those confusing \ characters if possible.

Any chance there is a tab character in the pattern? Maybe there is something about the pattern that is "funny" and is confusing us. You might try to find the pattern with grep first, to figure out the regular expression. Another option is to experiment with the final version above with simple pattern, verify that works, then move on to the longer pattern.

1 Like

Thanks a lot, it is working now. It was of great help :slight_smile:

Great. The way you are using sed is ideal. sed is so powerful for just this kind of discrete task. And it's kind of fun.

A lot of the power and challenge of sed is getting as good as possible at regular expressions. You can do a lot with a short script and a well-written regular expression, as in the case of the script you are using.