Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 4 down vote accepted

You should use:

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

Notice the escaped parenthesizes with a "\".

share|improve this answer
Or with GNU sed just add the -r (or --regexp-extended) switch. Anyway, with basic regexp you have to escape the + too. – manatwork Nov 27 '11 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! – RYN Nov 27 '11 at 18:54
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 Nov 28 '11 at 8:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.