1

I want to find a regex which contains the word Sat followed by one or more commas. I used this:

sed -e 's/Sat,+/Sat/ig' myfile.txt > output.txt

But it has no effect despite the fact that the file contains Sat,. Can you correct me?

qbq
  • 59

1 Answers1

0

The + is an extended regular expression symbol, while sed by default uses basic regular expressions.

In a basic regular expression, you may instead use \{1,\} or \+ (only GNU sed seems to know about \+ and it's not standard).

You may also switch sed to use extended expressions by using the -E option.

Related:

Kusalananda
  • 333,661
  • I am facing another problem. If I want to match multiple regex for all week days: sed -E 's/Sat,+/Sat/ig' -E 's/Sun,+/Sun/ig' -E 's/Mon,+/Mon/ig' It gives an errors: sed: can't read s/Sun,+/Sun/ig: No such file or directory. Can you plz point why? – qbq Jul 18 '19 at 19:50
  • 2
    @qbq, ah, -E doesn't work as an alternative to -e, you just need to add it to switch to extended regexps. So sed -E -e 's///' -e 's///' should do what you want. – ilkkachu Jul 18 '19 at 19:52
  • 1
    @qbq As ilkkachu said, or sed -E 's/(Mon|Tue|Wed|...|Sun),+/\1/ig' – Kusalananda Jul 18 '19 at 19:54