Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 2 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
2  
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! –  4r1y4n Nov 27 '11 at 18:54
1  
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
add comment

Your Answer

 
discard

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.