2

This is my first time attempting to use SED.

I can get the substitution command to work with simple strings. As in:

sed -i 's/foo/bar/g' file.txt

However when I attempt to use a Regex it stops working. The regex I want to use is trying to identify URLs:

(https?:)?(\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)

I am trying to remove all URLs from a text file. Here's my full command:

sed -i 's/(https?:)?(\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)//g' file.txt

Can anyone see the issue?

WillD
  • 183

1 Answers1

1

You may want to add -r to your sed command to turn on extended regular expressions.

In a nutshell, without -r, ?, +, (), {}, and | have literal meaning and you need to prefix them with a backslash to give them “regex” meaning.

See the documentation on the GNU website for a deeper explanation.

Patrice Levesque
  • 1,319
  • 10
  • 9