Sed/replace using variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed/replace using variable
# 1  
Old 11-24-2014
Sed/replace using variable

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
Code:
"/"

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,

Code:
NewDisk=`fdisk -l | grep LINUX | egrep -v '\*|LVM' | awk '{print $1}' | sed 's/\/dev\///'`

With above command the variable
Code:
$NewDisk

getting assigned value
Code:
csicc/c0d0p5

Now, I am doing sed/replace as below,

Code:
sed -i "s/^cdrom/harddrive --partition=${NewDisk} --dir=\//" FILENAME.txt

However this command is failing, I guess because the variable
Code:
$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,

Code:
sed: -e expression #1, char 38: unknown option to 's`

Even I tried to add escape before "/" as below,

Code:
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
Code:
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
# 2  
Old 11-24-2014
In sed you can replace the slash of the substitute command with almost any character .
Code:
$ echo "Turn forward // into dashes" | sed 's/\//-/g' 
Turn forward -- into dashes

Using | instead of / :
Code:
$ echo "Turn forward // into dashes" | sed 's|/|-|g' 
Turn forward -- into dashes

Another thing is that you may need extra backslashes within double quotes. So best to use single quotes..

Code:
$ echo "Turn forward // into backslashes" | sed 's/\//\\/g' 
Turn forward \\ into backslashes
$ echo "Turn forward // into backslashes" | sed "s/\//\\/g" 
sed: 1: "s/\//\/g": unterminated substitute in regular expression
$ echo "Turn forward // into backslashes" | sed "s/\//\\\\/g" 
Turn forward \\ into backslashes
$ echo "Turn forward // into backslashes" | sed 's|/|\\|g' 
Turn forward \\ into backslashes


Last edited by Scrutinizer; 11-24-2014 at 01:24 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-24-2014
If you're doing grep | sed | grep -v | awk | sed | cut | kitchen | sink, just use awk and be done with it.

fdisk prints 'Linux' on my system, not LINUX, but adjust to taste.

Code:
$ fdisk -l | awk '/Linux/ && !/[*]|LVM/ {sub("/dev/", ""); print $1 }'

sda1
sda2
sda3
sda5
sda6
sda7
sdb1
sdb2
sdb3
sdc1
sdc2
sdc3

$

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 11-24-2014
Don't see LINUX or LVM in the output of fdisk -l. Maybe you are using other than GNU Linux?

What I see here...
Code:
NewDisk=`fdisk -l | grep Linux | egrep -v '\*|LVM' |awk '{print $1}' | sed 's/\/dev\///'`
echo $NewDisk
# -- produces --
# sda5 sda6 sda7 sda10 sda11 sda12 sdf2 sdf5 sdf6 sdf7 sdf8 sdf9 sdf10 sdf11

# (using sed -e to debug)
sed -e "s/^cdrom/harddrive --partition=$NewDisk --dir=\//"
# -- produces --
# error

In my case, $NewDisk appears to sed as a space separated list of commands, which are bogus...
The fix for me would be to make $NewDisk an array and access by [index]
Code:
NewDisk=(`fdisk -l | grep Linux | egrep -v '\*|LVM' |awk '{print $1}' | sed 's/\/dev\///'`)
sed -e "s/^cdrom/harddrive --partition=${NewDisk[0]} --dir=\//"

In your case, making $NewDisk=csicc/c0d0p5 like you reported, still throws an error because sed sees the /slash in csicc/c0d0p5 as a delimiter...
So change the delimiter in sed from / to |...(ala Scrutinizer post #2)

Code:
NewDisk=csicc/c0d0p5
sed -e "s|^cdrom|harddrive --partition=$NewDisk --dir=\/|"
-- produces --
harddrive --partition=csicc/c0d0p5 --dir=/

Give it a go and lemme know if I need to make any edits here.

Thx

Last edited by ongoto; 11-24-2014 at 06:44 PM..
This User Gave Thanks to ongoto For This Post:
# 5  
Old 11-25-2014
Using
Code:
|

instead of
Code:
/

worked like charm.

Thanks a ton to Scrutinizer, Corona688 , ongoto !!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed to replace variable in file

Hi All I have one file with multiple lines in it, each line has static text and some variable enclosed in <<filename>> as well. e.g. as below 123, <<file1.txt>> this is my name, I stay at <<city.txt>> Thanks for visiting 348384y, this is my name <<fileabc.txt>>, I stay at near the mall of... (8 Replies)
Discussion started by: reldb
8 Replies

2. Shell Programming and Scripting

find and replace with variable -sed

Hi, I have a ksh script where I am trying to mask the password in the log files. $loc - is my directory $PGUIDE_DB_USER_PSW - is a variable that holds the password I am looking for find $loc/logs -type f -exec sed -i "s/$PGUIDE_DB_USER_PSW/*****/"g {} \; I get an error: ... (2 Replies)
Discussion started by: amitlib
2 Replies

3. Shell Programming and Scripting

Replace value of a variable in a file through script using sed

Hi, I am trying to replace the value of a variable in a file through another script. Example: Filename : abc.txt contents: a=10 b=20 c=30 Need to change the value of, say, b - Tried using the following: sed "s/${b}/15/g" abc.txt Have tried various forms of sed (with single quotes,... (4 Replies)
Discussion started by: rituparna_gupta
4 Replies

4. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

5. Shell Programming and Scripting

Using SED with variable to replace strings in while loop

Hi guys, Hi have this input (Menu.xml)<?xml version="1.0" encoding="ISO-8859-1"?> <breakfast_menu> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <calories>900</calories> </food> <food> <name>French Toast</name> ... (6 Replies)
Discussion started by: cgkmal
6 Replies

6. Shell Programming and Scripting

Replace with a variable in sed command

Hello, I have this command and it works fine. My question is that how can we replace the N by a variable, to print for instance a big number of lines. It means if I want 100 lines after an expression, to not put "N" 100 times in the sed. Code: $ sed -n '/aaa/{n;N;N;s///g;s/;/; /g;p;}'... (2 Replies)
Discussion started by: rany1
2 Replies

7. Shell Programming and Scripting

using file-and-replace sed command with the use of a variable?

Ok, so, let's say I have the variable $GMAILID....How can I use it with sed command so to replace a string in a file? e.g.: sed -i 's/$GMAILID/test@gmail.com/' /home/$USER/Desktop/sendmail (4 Replies)
Discussion started by: hakermania
4 Replies

8. Shell Programming and Scripting

sed replace spaces between quotes with a variable

I have lines with: elseif (req.http.host ~ "^(www.)?edificationtube.com$|www.edificationtube.org www.edificationtube.net edificationtube.org www.edificationtube.com edificationtube.net") { elseif (req.http.host ~ "^(www.)?collegecontender.com$|www.collegecontender.com collegecontenders.com... (3 Replies)
Discussion started by: EXT3FSCK
3 Replies

9. UNIX for Dummies Questions & Answers

sed to replace text with a variable

I'm trying to replace text in a file with text from a variable I have the following in my script, but its not working: #!/bin/ksh echo "Enter the path to load scripts" read x echo "updating the templates" sed "s/CHANGE_ME_TO_LOAD_PATH/"$x"/g" LoadFiles.sh > LoadFiles2.sh I thought... (1 Reply)
Discussion started by: orahi001
1 Replies

10. Shell Programming and Scripting

replace with value of variable using SED

Hello everyone, I need to replace a pattern with "value of a variable" using sed but no luck. i tried the below ABC=STR2 sed 's/STR1/$ABC/g' <file> (also tried \ , $ and " in combination but failed) Appreciate any help. Thanks Prvn (2 Replies)
Discussion started by: prvnrk
2 Replies
Login or Register to Ask a Question