Ankit Jain
Ankit Jain

Reputation: 1

Replace pattern in an existing line using sed

I want to replace the

/fdasatavol/ankit

to

/fdasatavol_sata/ankit

Can anyone help me out in this?

Upvotes: 0

Views: 90

Answers (2)

Chris Seymour
Chris Seymour

Reputation: 85883

This will replace each occurrence of fdasatavol with fdasatavol_sata:

sed 's/fdasatavol/&_sata/g'

If your input has occurrence of fdasatavol that are not in /fdasatavol/ankit and you don't want to substitute these then use:

sed 's@/fdasatavol/ankit@/fdastatavol_sata/ankit@g'

Note: you can use any character as sed's delimilter to aviod the confusion with the parrtern contiaing /. sed prints to stdout by default, if you are happy with the changes produced by sed you can use the -i option to store back to the file.

sed -i 's/fdasatavol/&_stat/g' file

Upvotes: 1

Lisa
Lisa

Reputation: 3536

to write to a new file (without modifying file1):

sed 's/fdasatavol/fdasatavol_sata/g' file1 > file2

or to replace in the original file:

sed -i 's/fdasatavol/fdasatavol_sata/g' file1

Upvotes: 1

Related Questions