sed search and replace with variable containing slash


up vote
6
down vote
favorite
I am trying to replace a string inside a text file with a variable.
I am using this code:
sed -i "s/search text /TAG//replace text $variable/" $textfile
The content of the variable is /newTAG/
. And the content of the variable is the problem, more accuratelyte the /
at the beginning and the end.
When both /
are removed so the content is only newTAG
everything works fine.
I already tried the following
sed -i "s/search text /TAG//replace text "$variable"/" $textfile
sed -i 's/search text /TAG//replace text "$variable"/' $textfile
Can anybody help me?
Thanks
command-line bash sed
add a comment |Â
up vote
6
down vote
favorite
I am trying to replace a string inside a text file with a variable.
I am using this code:
sed -i "s/search text /TAG//replace text $variable/" $textfile
The content of the variable is /newTAG/
. And the content of the variable is the problem, more accuratelyte the /
at the beginning and the end.
When both /
are removed so the content is only newTAG
everything works fine.
I already tried the following
sed -i "s/search text /TAG//replace text "$variable"/" $textfile
sed -i 's/search text /TAG//replace text "$variable"/' $textfile
Can anybody help me?
Thanks
command-line bash sed
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I am trying to replace a string inside a text file with a variable.
I am using this code:
sed -i "s/search text /TAG//replace text $variable/" $textfile
The content of the variable is /newTAG/
. And the content of the variable is the problem, more accuratelyte the /
at the beginning and the end.
When both /
are removed so the content is only newTAG
everything works fine.
I already tried the following
sed -i "s/search text /TAG//replace text "$variable"/" $textfile
sed -i 's/search text /TAG//replace text "$variable"/' $textfile
Can anybody help me?
Thanks
command-line bash sed
I am trying to replace a string inside a text file with a variable.
I am using this code:
sed -i "s/search text /TAG//replace text $variable/" $textfile
The content of the variable is /newTAG/
. And the content of the variable is the problem, more accuratelyte the /
at the beginning and the end.
When both /
are removed so the content is only newTAG
everything works fine.
I already tried the following
sed -i "s/search text /TAG//replace text "$variable"/" $textfile
sed -i 's/search text /TAG//replace text "$variable"/' $textfile
Can anybody help me?
Thanks
command-line bash sed
command-line bash sed
edited May 10 '17 at 17:49
muru
130k19275470
130k19275470
asked May 10 '17 at 9:25
Bird Person
332
332
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
9
down vote
accepted
Use something other than /
as the delimiter for the regex (for example, the colon :
):
sed -i "s:search text /TAG/:replace text $variable:" "$textfile"
Or escape the /
in the variable.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
Use something other than /
as the delimiter for the regex (for example, the colon :
):
sed -i "s:search text /TAG/:replace text $variable:" "$textfile"
Or escape the /
in the variable.
add a comment |Â
up vote
9
down vote
accepted
Use something other than /
as the delimiter for the regex (for example, the colon :
):
sed -i "s:search text /TAG/:replace text $variable:" "$textfile"
Or escape the /
in the variable.
add a comment |Â
up vote
9
down vote
accepted
up vote
9
down vote
accepted
Use something other than /
as the delimiter for the regex (for example, the colon :
):
sed -i "s:search text /TAG/:replace text $variable:" "$textfile"
Or escape the /
in the variable.
Use something other than /
as the delimiter for the regex (for example, the colon :
):
sed -i "s:search text /TAG/:replace text $variable:" "$textfile"
Or escape the /
in the variable.
answered May 10 '17 at 9:36
muru
130k19275470
130k19275470
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f913688%2fsed-search-and-replace-with-variable-containing-slash%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password