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.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
  • 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. – RavinderSingh13 Feb 26 '18 at 07: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. – RavinderSingh13 Feb 26 '18 at 08:00
  • Can you please explain me in details, I'm new to unix and have less knowledge in shell script @RavinderSingh13 – Gulam Jeelani Feb 26 '18 at 09:29

2 Answers2

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.

simon3270
  • 722
  • 1
  • 5
  • 8
  • 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 :: – Gulam Jeelani Feb 26 '18 at 10:32
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.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 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 – Gulam Jeelani Mar 01 '18 at 17:51
  • 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. – Ed Morton Mar 01 '18 at 18:44
  • 1
    I'll look over the code and try to implement it in my code,Thank you @Ed Morton – Gulam Jeelani Mar 02 '18 at 14:54
  • You're welcome and, of course, feel free to post a new question on this site if you run into any issues. – Ed Morton Mar 02 '18 at 15:27