Using regular expressions with sed (avoiding the "illegal reference \1" error)

Nothing beats sed for doing quick replacements on the command line, but if you try to naively use backreferences, e.g.

  sed "s/(thursday|friday)/\1 next week/" 

 you'll get the (not very helpful) error message

illegal reference \1

The solution is simple - just add the -r command line switch (or --regexp-extended if you like being verbose):

  sed -r "s/(thursday|friday)/\1 next week/"


107 Words

2015-04-01T05:24:00.002-07:00