Hello everyone,
In one of my shell script I am doing sed/replace using a variable to find a string in a file & replace it with another string in same file.
The challenge which I am facing is that, the variable which I am using with "sed" is having one of character as
"/"
Here is below how I am using the same,
Firstly, I am parsing "fdisk -l" output so that I will get the output in below format & assiging it to variable as below,
NewDisk=`fdisk -l | grep LINUX | egrep -v '\*|LVM' | awk '{print $1}' | sed 's/\/dev\///'`
With above command the variable
$NewDisk
getting assigned value
csicc/c0d0p5
Now, I am doing sed/replace as below,
sed -i "s/^cdrom/harddrive --partition=${NewDisk} --dir=\//" FILENAME.txt
However this command is failing, I guess because the variable
$NewDisk
contains "/" & which need to be escaped somehow, which I don't know how to do it.
I tried various options but no luck, every time it is failing with below error,
sed: -e expression #1, char 38: unknown option to 's`
Even I tried to add escape before "/" as below,
fdisk -l | grep LINUX | egrep -v '\*|LVM' | awk '{print $1}' | sed 's/\/dev\///' | sed "s/\//\\\//g"
when I run above command, on command line it works fine & display output as
csicc\/c0d0p5
, but it fails with above error when I try to get this in variable.
Please suggest me how to fix this.
Regards,
Uday