Replace text with another
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
I would like to replace a string with another string. For example:
I would like to change the following text:
"listen_addresses = ['127.0.0.1:53', '[::1]:53']"
to the following text
"listen_addresses = ['127.0.0.1:40', '[::1]:40']"
I tried this command:
sudo sed "s#listen_addresses = ['127.0.0.1:53', '[::1]:53']'#listen_addresses = ['127.0.0.1:40', '[::1]:40']#g"
and I got this result:
input: listen_addresses = ['127.0.0.1:53', '[::1]:53']
output: listen_addresses = ['127.0.0.1:53', '[::1]:53']
What am I doing wrong?
sed regular-expression replace
add a comment |Â
up vote
-1
down vote
favorite
I would like to replace a string with another string. For example:
I would like to change the following text:
"listen_addresses = ['127.0.0.1:53', '[::1]:53']"
to the following text
"listen_addresses = ['127.0.0.1:40', '[::1]:40']"
I tried this command:
sudo sed "s#listen_addresses = ['127.0.0.1:53', '[::1]:53']'#listen_addresses = ['127.0.0.1:40', '[::1]:40']#g"
and I got this result:
input: listen_addresses = ['127.0.0.1:53', '[::1]:53']
output: listen_addresses = ['127.0.0.1:53', '[::1]:53']
What am I doing wrong?
sed regular-expression replace
you only need to escape[
and]
, and in first part of replacement.
â Archemar
Sep 3 at 12:17
3
You're not "replacing any string", you're replacing a very specific string -- the port at the end of a listen_address configuration list in a file. Understanding exactly what you want leads people a long way towards the correct answer.
â Jeff Schaller
Sep 3 at 12:20
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I would like to replace a string with another string. For example:
I would like to change the following text:
"listen_addresses = ['127.0.0.1:53', '[::1]:53']"
to the following text
"listen_addresses = ['127.0.0.1:40', '[::1]:40']"
I tried this command:
sudo sed "s#listen_addresses = ['127.0.0.1:53', '[::1]:53']'#listen_addresses = ['127.0.0.1:40', '[::1]:40']#g"
and I got this result:
input: listen_addresses = ['127.0.0.1:53', '[::1]:53']
output: listen_addresses = ['127.0.0.1:53', '[::1]:53']
What am I doing wrong?
sed regular-expression replace
I would like to replace a string with another string. For example:
I would like to change the following text:
"listen_addresses = ['127.0.0.1:53', '[::1]:53']"
to the following text
"listen_addresses = ['127.0.0.1:40', '[::1]:40']"
I tried this command:
sudo sed "s#listen_addresses = ['127.0.0.1:53', '[::1]:53']'#listen_addresses = ['127.0.0.1:40', '[::1]:40']#g"
and I got this result:
input: listen_addresses = ['127.0.0.1:53', '[::1]:53']
output: listen_addresses = ['127.0.0.1:53', '[::1]:53']
What am I doing wrong?
sed regular-expression replace
sed regular-expression replace
edited Sep 10 at 5:47
DarkHeart
3,38822137
3,38822137
asked Sep 3 at 12:09
a.gulcan
113
113
you only need to escape[
and]
, and in first part of replacement.
â Archemar
Sep 3 at 12:17
3
You're not "replacing any string", you're replacing a very specific string -- the port at the end of a listen_address configuration list in a file. Understanding exactly what you want leads people a long way towards the correct answer.
â Jeff Schaller
Sep 3 at 12:20
add a comment |Â
you only need to escape[
and]
, and in first part of replacement.
â Archemar
Sep 3 at 12:17
3
You're not "replacing any string", you're replacing a very specific string -- the port at the end of a listen_address configuration list in a file. Understanding exactly what you want leads people a long way towards the correct answer.
â Jeff Schaller
Sep 3 at 12:20
you only need to escape
[
and ]
, and in first part of replacement.â Archemar
Sep 3 at 12:17
you only need to escape
[
and ]
, and in first part of replacement.â Archemar
Sep 3 at 12:17
3
3
You're not "replacing any string", you're replacing a very specific string -- the port at the end of a listen_address configuration list in a file. Understanding exactly what you want leads people a long way towards the correct answer.
â Jeff Schaller
Sep 3 at 12:20
You're not "replacing any string", you're replacing a very specific string -- the port at the end of a listen_address configuration list in a file. Understanding exactly what you want leads people a long way towards the correct answer.
â Jeff Schaller
Sep 3 at 12:20
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
as explain in comment, only [
and ]
are to be escaped
echo "listen_addresses = ['127.0.0.1:53', '[::1]:53']" |
sed -e "s#listen_addresses = ['127.0.0.1:53', '[::1]:53'#listen_addresses = ['127.0.0.1:40', '[::1]:40'#"
which give
listen_addresses = ['127.0.0.1:40', '[::1]:40']
Thank u for answer yes it's work
â a.gulcan
Sep 3 at 12:28
add a comment |Â
up vote
0
down vote
You can do:
sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
- Searches for the exact string
listen_addresses = ['127.0.0.1:53', '[::1]:53']
- If the string matches
s/53/40/g
will replace all occurences of53
with40
(but only in the matched line)
Example output:
$ cat file
listen_addresses = ['127.0.0.1:53', '[::1]:53']
listen_addresses = ['127.0.0.2:53', '[::1]:53']
$ sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
listen_addresses = ['127.0.0.1:40', '[::1]:40']
listen_addresses = ['127.0.0.2:53', '[::1]:53']
As you can see, an exact match is needed to perform the replace.
add a comment |Â
up vote
0
down vote
sed s/":53'"/":40'"/g <<<"listen_addresses = ['127.0.0.1:53', '[::1]:53']"
do something close to you asked. But I agree with comment of @Jeff Schaller!
From your question nobody can recognize why do you simply do not it by hand? What is the text from which you get your lines, how they differs each other? what variants should be covered by regex?
sed
is good for replacing but for electing the coresct line the grep
is better.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
as explain in comment, only [
and ]
are to be escaped
echo "listen_addresses = ['127.0.0.1:53', '[::1]:53']" |
sed -e "s#listen_addresses = ['127.0.0.1:53', '[::1]:53'#listen_addresses = ['127.0.0.1:40', '[::1]:40'#"
which give
listen_addresses = ['127.0.0.1:40', '[::1]:40']
Thank u for answer yes it's work
â a.gulcan
Sep 3 at 12:28
add a comment |Â
up vote
1
down vote
accepted
as explain in comment, only [
and ]
are to be escaped
echo "listen_addresses = ['127.0.0.1:53', '[::1]:53']" |
sed -e "s#listen_addresses = ['127.0.0.1:53', '[::1]:53'#listen_addresses = ['127.0.0.1:40', '[::1]:40'#"
which give
listen_addresses = ['127.0.0.1:40', '[::1]:40']
Thank u for answer yes it's work
â a.gulcan
Sep 3 at 12:28
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
as explain in comment, only [
and ]
are to be escaped
echo "listen_addresses = ['127.0.0.1:53', '[::1]:53']" |
sed -e "s#listen_addresses = ['127.0.0.1:53', '[::1]:53'#listen_addresses = ['127.0.0.1:40', '[::1]:40'#"
which give
listen_addresses = ['127.0.0.1:40', '[::1]:40']
as explain in comment, only [
and ]
are to be escaped
echo "listen_addresses = ['127.0.0.1:53', '[::1]:53']" |
sed -e "s#listen_addresses = ['127.0.0.1:53', '[::1]:53'#listen_addresses = ['127.0.0.1:40', '[::1]:40'#"
which give
listen_addresses = ['127.0.0.1:40', '[::1]:40']
answered Sep 3 at 12:25
Archemar
19.1k93467
19.1k93467
Thank u for answer yes it's work
â a.gulcan
Sep 3 at 12:28
add a comment |Â
Thank u for answer yes it's work
â a.gulcan
Sep 3 at 12:28
Thank u for answer yes it's work
â a.gulcan
Sep 3 at 12:28
Thank u for answer yes it's work
â a.gulcan
Sep 3 at 12:28
add a comment |Â
up vote
0
down vote
You can do:
sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
- Searches for the exact string
listen_addresses = ['127.0.0.1:53', '[::1]:53']
- If the string matches
s/53/40/g
will replace all occurences of53
with40
(but only in the matched line)
Example output:
$ cat file
listen_addresses = ['127.0.0.1:53', '[::1]:53']
listen_addresses = ['127.0.0.2:53', '[::1]:53']
$ sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
listen_addresses = ['127.0.0.1:40', '[::1]:40']
listen_addresses = ['127.0.0.2:53', '[::1]:53']
As you can see, an exact match is needed to perform the replace.
add a comment |Â
up vote
0
down vote
You can do:
sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
- Searches for the exact string
listen_addresses = ['127.0.0.1:53', '[::1]:53']
- If the string matches
s/53/40/g
will replace all occurences of53
with40
(but only in the matched line)
Example output:
$ cat file
listen_addresses = ['127.0.0.1:53', '[::1]:53']
listen_addresses = ['127.0.0.2:53', '[::1]:53']
$ sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
listen_addresses = ['127.0.0.1:40', '[::1]:40']
listen_addresses = ['127.0.0.2:53', '[::1]:53']
As you can see, an exact match is needed to perform the replace.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can do:
sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
- Searches for the exact string
listen_addresses = ['127.0.0.1:53', '[::1]:53']
- If the string matches
s/53/40/g
will replace all occurences of53
with40
(but only in the matched line)
Example output:
$ cat file
listen_addresses = ['127.0.0.1:53', '[::1]:53']
listen_addresses = ['127.0.0.2:53', '[::1]:53']
$ sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
listen_addresses = ['127.0.0.1:40', '[::1]:40']
listen_addresses = ['127.0.0.2:53', '[::1]:53']
As you can see, an exact match is needed to perform the replace.
You can do:
sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
- Searches for the exact string
listen_addresses = ['127.0.0.1:53', '[::1]:53']
- If the string matches
s/53/40/g
will replace all occurences of53
with40
(but only in the matched line)
Example output:
$ cat file
listen_addresses = ['127.0.0.1:53', '[::1]:53']
listen_addresses = ['127.0.0.2:53', '[::1]:53']
$ sed "/^listen_addresses = ['127.0.0.1:53', '[::1]:53']$/s/53/40/g" file
listen_addresses = ['127.0.0.1:40', '[::1]:40']
listen_addresses = ['127.0.0.2:53', '[::1]:53']
As you can see, an exact match is needed to perform the replace.
answered Sep 3 at 12:24
chaos
34.1k770113
34.1k770113
add a comment |Â
add a comment |Â
up vote
0
down vote
sed s/":53'"/":40'"/g <<<"listen_addresses = ['127.0.0.1:53', '[::1]:53']"
do something close to you asked. But I agree with comment of @Jeff Schaller!
From your question nobody can recognize why do you simply do not it by hand? What is the text from which you get your lines, how they differs each other? what variants should be covered by regex?
sed
is good for replacing but for electing the coresct line the grep
is better.
add a comment |Â
up vote
0
down vote
sed s/":53'"/":40'"/g <<<"listen_addresses = ['127.0.0.1:53', '[::1]:53']"
do something close to you asked. But I agree with comment of @Jeff Schaller!
From your question nobody can recognize why do you simply do not it by hand? What is the text from which you get your lines, how they differs each other? what variants should be covered by regex?
sed
is good for replacing but for electing the coresct line the grep
is better.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
sed s/":53'"/":40'"/g <<<"listen_addresses = ['127.0.0.1:53', '[::1]:53']"
do something close to you asked. But I agree with comment of @Jeff Schaller!
From your question nobody can recognize why do you simply do not it by hand? What is the text from which you get your lines, how they differs each other? what variants should be covered by regex?
sed
is good for replacing but for electing the coresct line the grep
is better.
sed s/":53'"/":40'"/g <<<"listen_addresses = ['127.0.0.1:53', '[::1]:53']"
do something close to you asked. But I agree with comment of @Jeff Schaller!
From your question nobody can recognize why do you simply do not it by hand? What is the text from which you get your lines, how they differs each other? what variants should be covered by regex?
sed
is good for replacing but for electing the coresct line the grep
is better.
answered Sep 3 at 12:43
schweik
1804
1804
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
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%2funix.stackexchange.com%2fquestions%2f466562%2freplace-text-with-another%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
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');
);
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');
);
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
you only need to escape
[
and]
, and in first part of replacement.â Archemar
Sep 3 at 12:17
3
You're not "replacing any string", you're replacing a very specific string -- the port at the end of a listen_address configuration list in a file. Understanding exactly what you want leads people a long way towards the correct answer.
â Jeff Schaller
Sep 3 at 12:20