1

Goal: I want to replace a string with content of file which has unix code.

What I'm trying to do is:

First: I'm storing the contents of file to a variable

variable=`cat sample.sh`
variable2="$(sed -z 's:\n:\\\n:g;$s:\\\n$::'<<<"$variable")" 

and then using that variable in sed command to replace the text with variable value.

sed -i "s:SearchString:$variable2//&/\\\&}:g"<<<"SearchString"  File.html

The sed command throws error stating that

sed: -e expression #, char 5: unknown option to s'

Please help to achieve my target.

3
  • 3
    Welcome to Stack overflow, please add 3 simple things, 1- Add sample Input file in code tags in your post. 2- Add expected output with all conditions with CODE TAGS, 3- what you have tried. Add these in your post and let us know then. Commented Feb 26, 2018 at 7:56
  • 2
    You need not to put your file's content into a variable and then reach variable with sed. sed itself could read the Input_file. Commented Feb 26, 2018 at 8:00
  • Can you please explain me in details, I'm new to unix and have less knowledge in shell script @RavinderSingh13 Commented Feb 26, 2018 at 9:29

2 Answers 2

0

The problem is probably that you have a colon in $variable2, and that is terminating the substitution value. Is there a character that you know won't be in $variable2? For example, the "pipe" character "|" - if there is, use that character here rather than ":" to separate the parts of the "s" command.

I assume it's a typo, but you are missing the opening curly brace in "${variable2" in the sed command.

There are other ways to solve your problem, but this is the minuimum change.

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

1 Comment

you are right.. colon was terminating the substitution @simon3270, I used caret (^) character to separate the parts. Can you please suggest me any char/symbol which will not likely to be used in shell script(my input file is shell file), can I use the combination of symbols? like @@ or ::
0

@GulamJeelani you're approaching this the wrong way with the wrong tool. sed is for doing s/old/new, that is all. See Is it possible to escape regex metacharacters reliably with sed for some of the problems you'll face trying to force sed to do this job or anything else more complicated than s/old/new. Just use awk:

awk '
BEGIN { old = "SearchString" }
NR==FNR { new = (FNR>1 ? new ORS : "") $0; next }
s=index($0,old) { $0 = substr($0,1,s-1) new substr($0,s+length(old)) }
{ print }
' sample.sh File.html

The above is untested of course since you didn't provide sample input/output that we could test against but it should be very obvious what it's doing and how to tweak it if necessary.

4 Comments

Before appending sample.sh to HTML file I'm doing some processing on that sh file, So I'm holding it into some variable and then running the sed command, just FYI I have more number of files (15-20) and using awk will make my code look lengthier. @Ed Morton
If it does then you're almost certainly approaching the task the wrong way. More importantly though - using awk will work robustly, efficiently, portably, and be trivial to enhance later which should all be far more important than whether or not your code is a couple of lines longer even if that were the case. In your comment under the answer you accepted you said suggest me any char/symbol which will not likely to be used in shell script - that alone should be a huge red flag to you that your approach is wrong.
I'll look over the code and try to implement it in my code,Thank you @Ed Morton
You're welcome and, of course, feel free to post a new question on this site if you run into any issues.

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.