SED: replacing

Hello,

I was looking around, but could not find the answer, so I hope you ppl can help me.

I want simply to replace text.:rolleyes:
I found out SED would be good for this task.:b:

So I tried: :confused:

1.) find text in a line and replace this particular line:
for finding searchstring as part of line e.g.: this line contains a searchstring bla bla...?!
And whole line should be replaced by "replaces_the_old_line_with_this"
(makefile is in current folder - file is writeable)

sed -e '/searchedstring/c\replaces_the_old_line_with_this' makefile

2.) replace text thorugh other text:

sed -e '/include \$(BUILDPATH)\/FOLDER1\/FOLDER2\/c\ TEXT \$(BUILDPATH)\/FOLDER-CHANGED\/ANOTHERFOLDER\/BLA_FOLDER\/FILENAME' makefile

both times I get this error: "sed: command garbled: ..."

so can you tell me waht i do wrong?
especially the "$" and "/" could make errors. But i tried it also for a simple case liek in nr.1 and this didn't work as well.

Thanks a lot.
Best regards!

Can you post a input and expected output here

Assuming that $(BUILDPATH) is a command that returns a string. You need to take a look at what your shell needs concerning quotation and shell variables in sed.
Highlighting some points you should take a look

sed -e "/include \$(BUILDPATH)\/FOLDER1\/FOLDER2\/c\ TEXT \$(BUILDPATH)\/FOLDER-CHANGED\/ANOTHERFOLDER\/BLA_FOLDER\/FILENAME" makefile

Missing / to complete the first / for matching

1 Like

let me start with basics... to ensure sed works at my machine... simple text replacement..

replace old_string with new_string (just was written word)

my test file: file1 in dir - where i'm at the moment i start sed command

my command:
sed -e 's/old_string/new_string;' file1
or
sed -e "s/old_string/new_string" file1

bote does not work:
error: sed -e "s/old_string/new_string" file1

thanks for help

---------- Post updated at 01:45 PM ---------- Previous update was at 12:04 PM ----------

reading helps :wink:

I really shouldn't skip the first information in the man pages :wall:...
it says: "SED edits in a file and gives output to stream."
That gnerated my problem...

so this comand works... for all who are walking into same trouble and because a thread should always have a solution at the end :wink:

command works:

sed -e 's/old_string/new_string/g' file1 > tmpfile1 && mv tmpfile1 file1

so I'll have a try for my real problem with patchs and other vars but should work now. Just could not start to work on the problem, because the error information did not help me.

---------- Post updated at 05:02 PM ---------- Previous update was at 01:45 PM ----------

works - issue solved (using backslash for masking path slash and for variable $)
thx