Skip to content
Advertisement

+ Regular Expression not working in sed

Team,

I have a file with data as mention below.

[root@ip-12-32-8-15 ~]# cat dbbackup

(555)555-1212

(555)555-1213

Expected Output:

First: (555) Second: 555- Third: 1212

First: (555) Second: 555- Third: 1213

I am using sed its not working .

sed -n -e 's/([0-9]+))([0-9]+-)([0-9]+)/First: 1 Second: 2 Third: 3/p' dbbackup

Please Let me know how to achieve the above mention output.

Advertisement

Answer

This might work for you (GNU sed):

sed -r 's/$/nfirst:second:third:fourth:n/;:a;s/^([(0-9]+[)-]?)([^n]*n)([^:]*:)(.*)/24 3 1/;ta;s/n.*n //' file

This method matches the numbers at the front of the string with strings appended to the end of the line and then appends them to end of the introduced line. Eventually there are no more numbers to remove and then the introduced newlines/final space are deleted, leaving the required string.

N.B. Additional strings may be appended for further numbers.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement