Sponsored Content
Full Discussion: Sed/replace using variable
Top Forums Shell Programming and Scripting Sed/replace using variable Post 302926431 by gr8_usk on Monday 24th of November 2014 12:09:54 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 01:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy