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?
sed -E 's/(\(555\))(.*-)(.*)/Area code: \1 Second: \2 Third: \3/'
. With POSIX ERE syntax, you may escape parentheses as in all common online regex testers. – Dinh(
to get a literal parenthesis; just use the parenthesis on its own, so(
instead of\\\\(
. – Musser