-2

I am trying to replace string with a sentence or paragraph contacting multiple newline characters using shell script. Replacement string will be generated run-time.

eg:

sed /string_to_be_replaced/Replacement(newline character) string (newline character)/g

Any inputs/ideas are welcome. Thanks!

  • 5
    please add an input and output of want you want to describe/solve – fusion.slope Oct 04 '17 at 18:26
  • For random values of the replacement, this can not be done through sed as the delimiter for the substitution command need to use a character that is guaranteed to not occur in the pattern nor replacement. One would need to pre-process the string first. – Kusalananda Oct 04 '17 at 19:37
  • We have file_1 with multiple placeholders/tags. file_2 will have data feed which will be generated by different team altogether. Data on file_2 may contain multiple sentences with special characters like newline,tab,&,$(basically anything) and | will be used as delimiter for this.. My objective is to replace the placeholders on file_1 from file_2 feed. – naveen sharma Oct 04 '17 at 22:38

1 Answers1

3

Using GNU sed.

mline="this is\na line\nin multiple\nlines"
sed "s/PATTERN/${mline}/g" <<<"PATTERN here."
this is
a line
in multiple
lines here.

If your input contains / as special character or & which will match as pattern match in sed. Use global Pattern Substitution // to replace/escape all /es and replace back all PATTERNs with \&.

sed "s/PATTERN/${mline//\//\\/}/g; s/PATTERN/\&/" <<<"PATTERN here."

Or either better to use different sed's substation delimiter and escape & again.

sed "s:PATTERN:${mline//&/\\&}:g" <<<"PATTERN here."

And finally if you want it works on actual Enter, first we need to replace all \n with \\n to let them feed to sed as \n. So

Input in multiple lines with actual enter.

mline="th&is is
a line
in mul/tiple
line/s"

The command:

aline="$(sed -z 's:\n:\\n:g;$s:\\n$::' <<<"$mline")
sed "s:PATTERN:${aline//&/\\&}:g" <<<"PATTERN here."

The output is:

th&is is
a line
in mul/tiple
line/s here.
αғsнιη
  • 41,407
  • I have tried this solution but it will not work with newline(enter key) – naveen sharma Oct 04 '17 at 22:43
  • You should perhaps use | as separator for the s command. According to a comment by the OP this is used as delimiter in the replacement file, so it seems not to be part of a replacement. Anyhow, the OP needs to add sample data. – Philippos Oct 05 '17 at 08:28
  • Reserved characters means any of \&/ plus any embedded newlines(where / is the delimiter and since you cannot know what the replacement string will contain you're not gaining anything if you use another delimiter as you'll have to escape it anyway)... You are complicating things here IMO (and relying on gnu sed's z which is not mentioned in the Q)... anyay, this Q is a duplicate as it has been asked several times (and at least one of the duplicates has the right answer). – don_crissti Oct 05 '17 at 10:32
  • @don_crissti If this is a reply to my comment: Yes, we know that the replacement string will not contain |, so using this as delimiter will free you from caring about one of the pitfalls, while the : suggested in the answer doesn't. – Philippos Oct 05 '17 at 11:08