4

I am simply trying to replace text in bash, but having a hard time doing so. Other posts from years ago seem to result in an extremely complicated process, but I was hoping to get some easier and simpler assistance.

Here's the text that I have:

Here is a random sentence that contains [ABC-123](https://app.website.com/random/path/here?query=1) somewhere in the middle of it.

Here's what I'm trying to replace in the above text:

➜  ~ echo $replace_string
[ABC-123](https://app.website.com/random/path/here?query=1)

I am trying to replace it with this:

➜  ~ echo $replace_with
<https://app.website.com/random/path/here?query=1|[ABC-123]>

I am ultimately trying to convert Markdown interpreted by GitHub into Markdown that is interpreted by Slack (for whatever reason have their own way of doing this).

I have tried using sed, but this just does work because it keeps trying to interpret the replace strings as regex

➜  ~ echo $contents
Here is a random sentence that contains [ABC-123](https://app.website.com/random/path/here?query=1) somewhere in the middle of it.
➜  ~ echo $replace_string
[ABC-123](https://app.website.com/random/path/here?query=1)
➜  ~ echo $replace_with
<https://app.website.com/random/path/here?query=1|[ABC-123]>
➜  ~ echo $contents | sed "s/$replace_string/$replace_with/g"
sed: 1: "s/[ABC-123](https://app ...": RE error: invalid character range

Is there just a simple way to turn off regex interpretation in sed or another equivalent way using another utility? Other than modifying the text itself via sed, I am hoping to be able to use variables which hold the text, since the text will vary in many cases.

  • Use awk instead of sed. Awk has an index(str) function to find substrings, length() to measure the number of chars, and substr() to cut out and concatenate strings. None of those recognise any REs or special characters, so no issues. – Paul_Pedant Apr 17 '22 at 07:41

2 Answers2

1

You need then to escape those special characters and then it works:

sed 's@\[ABC-123](https://app.website.com/random/path/here?query=1)@<https://app.website.com/random/path/here?query=1|\[ABC-123]>@'

I used '@' as the syntax delimiter so that one doesn't need to escape every single '/' too.

seshoumara
  • 862
  • 5
  • 7
  • 1
    Even less escaping is necessary if you don't use -r (which enables extended regular expressions). Without -r, you would not have to escape (, ), |, or ? since you're dealing with basic regular expressions. Also note that &, if it occurs, must be escaped in the replacement text, as well as \n for any number n in both pattern and replacement. – Kusalananda Apr 17 '22 at 05:52
  • @Kusalananda thank you, I modified it. Very often I need to use capture groups, + and ?, so it has become a habit for me to start with -r. – seshoumara Apr 17 '22 at 06:02
  • Capture groups are written \(...\) in basic regular expressions and + and ? are just syntactic sugar for \{1,\} and \{0,1\}. Note too that in a future POSIX standard, the option to enable extended regular expressions in sed will most probably be -E rather than the GNU option -r. GNU sed understands both options. – Kusalananda Apr 17 '22 at 06:08
  • Unfortunately, the text varies and so I was hoping to use a variable when replacing the text. – user3447014 Apr 18 '22 at 01:46
0

It seems you could use pandoc with following filter https://github.com/omarish/pandoc-md-to-slack

Garid Z.
  • 542