user2432988
user2432988

Reputation: 25

Sed search one regex, return another

I have the following csv file:

hd1,100 
hd2,200

I'd like to change it so it reads like this:

hard1drive,100
hard2drive,200

I thought sed could help:

sed s'/hd[0-9]/hard[0-9]drive]/ < infile.csv

but instead of the desired output I get:

hard[0-9]drive,100
hard[0-9]drive,200

Is there any way I can 'capture' the number from the search parameter and insert it within the replace parameter within sed, or am I going to have to use another command?

Upvotes: 0

Views: 47

Answers (2)

Kent
Kent

Reputation: 195179

option without grouping:

kent$  echo "hd1,100 
hd2,200"|sed 's/d[0-9]/ar&drive/'                                                                                                                                           
hard1drive,100 
hard2drive,200

Upvotes: 0

iruvar
iruvar

Reputation: 23374

Use capturing groups

 sed 's/hd\([0-9]\)/hard\1drive/'

Upvotes: 4

Related Questions