2

My filename is myfile.txt and i want to replace some data inside it using shell script. I want to replace ScriptPathPC=\Myfile\file_core into ScriptPathPC=./file_core/ The code which i tried is

replace "ScriptPathPC=\Myfile\file_core" "ScriptPathPC=./file_core/" -- myfile.txt

But this command is working well with forward slash[/] and not with backward slash[]. Is there any other solution for this??

1 Answer 1

1

Using replace

replace requires that the substitution be done perl-style:

replace 's|ScriptPathPC=\\Myfile\\file_core|ScriptPathPC=./file_core/|' file.txt 

The substitute command looks like s|old|new|. To prevent special treatment of them, we have to escape the backslashes.

Using sed

sed can similarly make changes. Here, we display the new file on stdout:

$ sed 's|ScriptPathPC=\\Myfile\\file_core|ScriptPathPC=./file_core/|' myfile.txt 
ScriptPathPC=./file_core/

Here, we change the old file in place:

sed -i 's|ScriptPathPC=\Myfile\file_core|ScriptPathPC=./file_core/|' myfile.txt

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.