Skip to content
Advertisement

How to use back-reference of sed replacement command correctly considering a special Regular Expression

I am learning the sed s/regexp/replacement/ command on linux.

There are some numbers from phone.txt

(555)555-1212
(555)555-1213
(555)555-1214
(666)555-1215
(777)555-1217

I’d like to use the regular expression (which I have tested on https://www.freeformatter.com/regex-tester.html)

 ((555))(.*-)(.*$)

to match numbers which begin with (555). And then I want the output of these three parts of these matched number as: (an example for number (555)555-1212)

Area code: (555) Second: 555- Third: 1212

I tried the following command:

cat phone.txt | sed 's/(\(555\))(.*-)(.*$)/Area code: 1 Second: 2 Third: 3/'

But the system gave me:

sed: -e expression #1, char 66: Unmatched ( or (

The general command for all numbers was:

cat phone.txt | sed 's/(.*))(.*-)(.*$)/Area code: 1 Second: 2 Third: 3/'

Source: https://www.tutorialspoint.com/unix/unix-regular-expressions.htm

But I just want to execute sed on numbers which begins with (555) and add it to the output through back reference.

Could you tell me how to write this special command correctly?

Advertisement

Answer

Ypu are using POSIX BRE syntax in your sed command, and in such patterns, unescaped parentheses match literal parentheses. Escaped parentheses there define capturing groups.

You may use

sed -E 's/((555))(.*-)(.*)/Area code: 1 Second: 2 Third: 3/'

See the online demo

Literal parentheses in POSIX ERE syntax (enabled with -E option) are escaped as in all common online regex testers, and unescaped parentheses define capturing groups.

Advertisement