Friday, April 15, 2022

[SOLVED] bash sed replace a line number in a file containing a colon

Issue

I am on RHEL 7.3. Line 12 of myfile.txt looks like:

image: /currentimage/myimage

I need a bash script to change it to:

image: /newimage/otherimage

I tried doing it this way:

sed -i '12s/image: /currentimage/myimage/image: /newimage/otherimage/' ./myfile

But it fails with:

sed: unknown option to `s'

Solution

You are using / as your sed separator, AND it is used in your paths. Try using | as your separator instead.

sed -i '12s|image: /currentimage/myimage|image: /newimage/otherimage|' ./myfile

Additionally you can escape each / in the file path like so \/ .



Answered By - Kip K
Answer Checked By - Dawn Plyler (WPSolving Volunteer)