I'm trying to use sed command to find/replace a pattern in file.
It gives:

sed: -e expression #1, char 27: invalid reference \1 on `s' command's RHS

My command is:

sed 's/"([A-Z]+)",/"\1","\L\1"/g' file.txt 

What is the problem?
How can I fix it?

1 Answer 1


You should use:

sed 's/"\([A-Z]+\)",/"\1","\L\1"/g' file.txt

Notice the escaped parenthesizes with a "\".

  • 6
    Or with GNU sed just add the -r (or --regexp-extended) switch. Anyway, with basic regexp you have to escape the + too.
    – manatwork
    Commented Nov 27, 2011 at 18:42
  • But I'm using ' so i don't need escaping for shell itself. and for regex i'm using parenthesizes for grouping!
    – Ariyan
    Commented Nov 27, 2011 at 18:54
  • 2
    Those escaping are required by the basic regexp syntax. Not related to the used string delimiters – if you put the sed code in a separate file, the backslashes would be still required if you use basic regexp.
    – manatwork
    Commented Nov 28, 2011 at 8:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .