replace name after specific syntax
Clash Royale CLAN TAG#URR8PPP
up vote
-2
down vote
favorite
We have Bash script. We want to change the name - master02
with $machine_master
in the Bash script.
value=master02_up
#master02
http://master02.$domain:8080
How to change master02
to $machine_master
, only if master02
is after http://master02
.
Expected output:
value=master02_up
#master02
http://$machine_master.$domain:8080
bash awk sed regular-expression perl
add a comment |Â
up vote
-2
down vote
favorite
We have Bash script. We want to change the name - master02
with $machine_master
in the Bash script.
value=master02_up
#master02
http://master02.$domain:8080
How to change master02
to $machine_master
, only if master02
is after http://master02
.
Expected output:
value=master02_up
#master02
http://$machine_master.$domain:8080
bash awk sed regular-expression perl
1
awk '/http/ sub(/master02/,"$machine_master")1' file
â jasonwryan
Jul 5 at 7:48
1
is$machine_master
intended as variable or as literal string?
â RomanPerekhrest
Jul 5 at 7:59
@RomanPerekhrest From the expected output, it looks as if it's intended to be a fixed string.
â Kusalananda
Jul 5 at 9:54
@Kusalananda, there are many unexpected cases in comprehension of user questions. I feel like it's better to obtain the real "picture" through elaboration (even if something seems simple) (based on experience of working with SO and U&L)
â RomanPerekhrest
Jul 5 at 10:18
add a comment |Â
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
We have Bash script. We want to change the name - master02
with $machine_master
in the Bash script.
value=master02_up
#master02
http://master02.$domain:8080
How to change master02
to $machine_master
, only if master02
is after http://master02
.
Expected output:
value=master02_up
#master02
http://$machine_master.$domain:8080
bash awk sed regular-expression perl
We have Bash script. We want to change the name - master02
with $machine_master
in the Bash script.
value=master02_up
#master02
http://master02.$domain:8080
How to change master02
to $machine_master
, only if master02
is after http://master02
.
Expected output:
value=master02_up
#master02
http://$machine_master.$domain:8080
bash awk sed regular-expression perl
edited Jul 5 at 9:50
slmâ¦
233k65479651
233k65479651
asked Jul 5 at 7:43
yael
1,809941
1,809941
1
awk '/http/ sub(/master02/,"$machine_master")1' file
â jasonwryan
Jul 5 at 7:48
1
is$machine_master
intended as variable or as literal string?
â RomanPerekhrest
Jul 5 at 7:59
@RomanPerekhrest From the expected output, it looks as if it's intended to be a fixed string.
â Kusalananda
Jul 5 at 9:54
@Kusalananda, there are many unexpected cases in comprehension of user questions. I feel like it's better to obtain the real "picture" through elaboration (even if something seems simple) (based on experience of working with SO and U&L)
â RomanPerekhrest
Jul 5 at 10:18
add a comment |Â
1
awk '/http/ sub(/master02/,"$machine_master")1' file
â jasonwryan
Jul 5 at 7:48
1
is$machine_master
intended as variable or as literal string?
â RomanPerekhrest
Jul 5 at 7:59
@RomanPerekhrest From the expected output, it looks as if it's intended to be a fixed string.
â Kusalananda
Jul 5 at 9:54
@Kusalananda, there are many unexpected cases in comprehension of user questions. I feel like it's better to obtain the real "picture" through elaboration (even if something seems simple) (based on experience of working with SO and U&L)
â RomanPerekhrest
Jul 5 at 10:18
1
1
awk '/http/ sub(/master02/,"$machine_master")1' file
â jasonwryan
Jul 5 at 7:48
awk '/http/ sub(/master02/,"$machine_master")1' file
â jasonwryan
Jul 5 at 7:48
1
1
is
$machine_master
intended as variable or as literal string?â RomanPerekhrest
Jul 5 at 7:59
is
$machine_master
intended as variable or as literal string?â RomanPerekhrest
Jul 5 at 7:59
@RomanPerekhrest From the expected output, it looks as if it's intended to be a fixed string.
â Kusalananda
Jul 5 at 9:54
@RomanPerekhrest From the expected output, it looks as if it's intended to be a fixed string.
â Kusalananda
Jul 5 at 9:54
@Kusalananda, there are many unexpected cases in comprehension of user questions. I feel like it's better to obtain the real "picture" through elaboration (even if something seems simple) (based on experience of working with SO and U&L)
â RomanPerekhrest
Jul 5 at 10:18
@Kusalananda, there are many unexpected cases in comprehension of user questions. I feel like it's better to obtain the real "picture" through elaboration (even if something seems simple) (based on experience of working with SO and U&L)
â RomanPerekhrest
Jul 5 at 10:18
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Using standard sed
:
sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile
This replaces the exact string http://master02.$domain:8080
with http://$machine_master.$domain:8080
and writes the result to a new file.
The $
in $domain
has to be escaped to not be interpreted as an "end of line" pattern. The $
in the replacement text does not need to be escaped as this part is not a pattern.
I'm using #
as a delimiter for the sed
substitute command (s
) as the pattern and replacement text both contain /
which is the default delimiter.
The command could also be shortened to
sed 's#http://master02#http://$machine_master#' file >newfile
if it's safe to do so (this depends on the contents of the file and what instances of text you'd like to replace).
Testing:
$ cat file
value=master02_up
#master02
http://master02.$domain:8080
$ sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile
$ cat newfile
value=master02_up
#master02
http://$machine_master.$domain:8080
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Using standard sed
:
sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile
This replaces the exact string http://master02.$domain:8080
with http://$machine_master.$domain:8080
and writes the result to a new file.
The $
in $domain
has to be escaped to not be interpreted as an "end of line" pattern. The $
in the replacement text does not need to be escaped as this part is not a pattern.
I'm using #
as a delimiter for the sed
substitute command (s
) as the pattern and replacement text both contain /
which is the default delimiter.
The command could also be shortened to
sed 's#http://master02#http://$machine_master#' file >newfile
if it's safe to do so (this depends on the contents of the file and what instances of text you'd like to replace).
Testing:
$ cat file
value=master02_up
#master02
http://master02.$domain:8080
$ sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile
$ cat newfile
value=master02_up
#master02
http://$machine_master.$domain:8080
add a comment |Â
up vote
1
down vote
accepted
Using standard sed
:
sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile
This replaces the exact string http://master02.$domain:8080
with http://$machine_master.$domain:8080
and writes the result to a new file.
The $
in $domain
has to be escaped to not be interpreted as an "end of line" pattern. The $
in the replacement text does not need to be escaped as this part is not a pattern.
I'm using #
as a delimiter for the sed
substitute command (s
) as the pattern and replacement text both contain /
which is the default delimiter.
The command could also be shortened to
sed 's#http://master02#http://$machine_master#' file >newfile
if it's safe to do so (this depends on the contents of the file and what instances of text you'd like to replace).
Testing:
$ cat file
value=master02_up
#master02
http://master02.$domain:8080
$ sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile
$ cat newfile
value=master02_up
#master02
http://$machine_master.$domain:8080
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Using standard sed
:
sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile
This replaces the exact string http://master02.$domain:8080
with http://$machine_master.$domain:8080
and writes the result to a new file.
The $
in $domain
has to be escaped to not be interpreted as an "end of line" pattern. The $
in the replacement text does not need to be escaped as this part is not a pattern.
I'm using #
as a delimiter for the sed
substitute command (s
) as the pattern and replacement text both contain /
which is the default delimiter.
The command could also be shortened to
sed 's#http://master02#http://$machine_master#' file >newfile
if it's safe to do so (this depends on the contents of the file and what instances of text you'd like to replace).
Testing:
$ cat file
value=master02_up
#master02
http://master02.$domain:8080
$ sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile
$ cat newfile
value=master02_up
#master02
http://$machine_master.$domain:8080
Using standard sed
:
sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile
This replaces the exact string http://master02.$domain:8080
with http://$machine_master.$domain:8080
and writes the result to a new file.
The $
in $domain
has to be escaped to not be interpreted as an "end of line" pattern. The $
in the replacement text does not need to be escaped as this part is not a pattern.
I'm using #
as a delimiter for the sed
substitute command (s
) as the pattern and replacement text both contain /
which is the default delimiter.
The command could also be shortened to
sed 's#http://master02#http://$machine_master#' file >newfile
if it's safe to do so (this depends on the contents of the file and what instances of text you'd like to replace).
Testing:
$ cat file
value=master02_up
#master02
http://master02.$domain:8080
$ sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile
$ cat newfile
value=master02_up
#master02
http://$machine_master.$domain:8080
edited Jul 5 at 8:03
answered Jul 5 at 7:53
Kusalananda
101k13199312
101k13199312
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%2f453543%2freplace-name-after-specific-syntax%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
1
awk '/http/ sub(/master02/,"$machine_master")1' file
â jasonwryan
Jul 5 at 7:48
1
is
$machine_master
intended as variable or as literal string?â RomanPerekhrest
Jul 5 at 7:59
@RomanPerekhrest From the expected output, it looks as if it's intended to be a fixed string.
â Kusalananda
Jul 5 at 9:54
@Kusalananda, there are many unexpected cases in comprehension of user questions. I feel like it's better to obtain the real "picture" through elaboration (even if something seems simple) (based on experience of working with SO and U&L)
â RomanPerekhrest
Jul 5 at 10:18