Reputation: 145
Team,
I have a file with data as mention below.
[root@ip-12-32-8-15 ~]# cat dbbackup
(555)555-1212
(555)555-1213
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.
Upvotes: 0
Views: 112
Reputation: 58578
This might work for you (GNU sed):
sed -r 's/$/\nfirst:second:third:fourth:\n/;:a;s/^([(0-9]+[)-]?)([^\n]*\n)([^:]*:)(.*)/\2\4 \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.
Upvotes: 0
Reputation: 37464
In GNU awk (FIELDWIDTHS
), no regex:
$ awk 'BEGIN{FIELDWIDTHS="5 4 4"}{print "First:",$1,"Second:",$2,"Third:",$3}' file
First: (555) Second: 555- Third: 1212
First: (555) Second: 555- Third: 1213
Another in sed:
$ sed 's/^/First: /1;s/./& Second: /12;s/./& Third: /25' file
First: (555) Second: 555- Third: 1212
First: (555) Second: 555- Third: 1213
Upvotes: 1
Reputation: 72425
You forgot the opening parenthesis before the first field. But this is not the reason it doesn't work.
It doesn't work because sed
does not know +
. Use *
instead:
sed -n -e 's/\(([0-9]*)\)\([0-9]*-\)\([0-9]*\)/First: \1 Second: \2 Third: \3/p'
Upvotes: 0