sed : replace 1 word without repeating the rest of the sentence
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have the following pattern :
foo bar
with foo
being always the same, but bar
can change. I have an hash table that, for each value of bar
, has a new one. What I do now is:
sed -i 's/foo bar/foo new_value/g' file
I am pretty sure there is a way to avoid repeating the foo
in the replacement string. Does sed
provide something like this ? Like
sed -i 's/foo @@bar@@/new_value/g' file
@@string@@
being the value that has to be replaced.
sed
add a comment |
up vote
1
down vote
favorite
I have the following pattern :
foo bar
with foo
being always the same, but bar
can change. I have an hash table that, for each value of bar
, has a new one. What I do now is:
sed -i 's/foo bar/foo new_value/g' file
I am pretty sure there is a way to avoid repeating the foo
in the replacement string. Does sed
provide something like this ? Like
sed -i 's/foo @@bar@@/new_value/g' file
@@string@@
being the value that has to be replaced.
sed
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have the following pattern :
foo bar
with foo
being always the same, but bar
can change. I have an hash table that, for each value of bar
, has a new one. What I do now is:
sed -i 's/foo bar/foo new_value/g' file
I am pretty sure there is a way to avoid repeating the foo
in the replacement string. Does sed
provide something like this ? Like
sed -i 's/foo @@bar@@/new_value/g' file
@@string@@
being the value that has to be replaced.
sed
I have the following pattern :
foo bar
with foo
being always the same, but bar
can change. I have an hash table that, for each value of bar
, has a new one. What I do now is:
sed -i 's/foo bar/foo new_value/g' file
I am pretty sure there is a way to avoid repeating the foo
in the replacement string. Does sed
provide something like this ? Like
sed -i 's/foo @@bar@@/new_value/g' file
@@string@@
being the value that has to be replaced.
sed
sed
edited Nov 25 at 15:13
Rui F Ribeiro
38.3k1476127
38.3k1476127
asked Oct 24 '14 at 9:07
EddaSnorra
1132
1132
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
sed -i 's/(foo )bar/1new_value/g' file
The ( )
sequence "captures" the text which then can be placed in the replacement string as 1
. This can be done multiple times, the digit in 1
indicates which captured text to use. Captured text is counted in order of (
order.
Please mark the answer as correct if it works for you :-)
– wurtel
Oct 24 '14 at 9:13
I will as soon as I can (10 minute delay).
– EddaSnorra
Oct 24 '14 at 9:17
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
sed -i 's/(foo )bar/1new_value/g' file
The ( )
sequence "captures" the text which then can be placed in the replacement string as 1
. This can be done multiple times, the digit in 1
indicates which captured text to use. Captured text is counted in order of (
order.
Please mark the answer as correct if it works for you :-)
– wurtel
Oct 24 '14 at 9:13
I will as soon as I can (10 minute delay).
– EddaSnorra
Oct 24 '14 at 9:17
add a comment |
up vote
0
down vote
accepted
sed -i 's/(foo )bar/1new_value/g' file
The ( )
sequence "captures" the text which then can be placed in the replacement string as 1
. This can be done multiple times, the digit in 1
indicates which captured text to use. Captured text is counted in order of (
order.
Please mark the answer as correct if it works for you :-)
– wurtel
Oct 24 '14 at 9:13
I will as soon as I can (10 minute delay).
– EddaSnorra
Oct 24 '14 at 9:17
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
sed -i 's/(foo )bar/1new_value/g' file
The ( )
sequence "captures" the text which then can be placed in the replacement string as 1
. This can be done multiple times, the digit in 1
indicates which captured text to use. Captured text is counted in order of (
order.
sed -i 's/(foo )bar/1new_value/g' file
The ( )
sequence "captures" the text which then can be placed in the replacement string as 1
. This can be done multiple times, the digit in 1
indicates which captured text to use. Captured text is counted in order of (
order.
answered Oct 24 '14 at 9:10
wurtel
9,66011325
9,66011325
Please mark the answer as correct if it works for you :-)
– wurtel
Oct 24 '14 at 9:13
I will as soon as I can (10 minute delay).
– EddaSnorra
Oct 24 '14 at 9:17
add a comment |
Please mark the answer as correct if it works for you :-)
– wurtel
Oct 24 '14 at 9:13
I will as soon as I can (10 minute delay).
– EddaSnorra
Oct 24 '14 at 9:17
Please mark the answer as correct if it works for you :-)
– wurtel
Oct 24 '14 at 9:13
Please mark the answer as correct if it works for you :-)
– wurtel
Oct 24 '14 at 9:13
I will as soon as I can (10 minute delay).
– EddaSnorra
Oct 24 '14 at 9:17
I will as soon as I can (10 minute delay).
– EddaSnorra
Oct 24 '14 at 9:17
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f163958%2fsed-replace-1-word-without-repeating-the-rest-of-the-sentence%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown