How to use sed regex to replace to words related to each other and one character between them
up vote
1
down vote
favorite
How to use sed regex to replace to words related to each other and one character between them with out change the character and the two words as group
like that
ahmed#mohamed
ahmed$mohamed
ahmed7mohamed
I didn't want to replace ahmed
only and then replace mohamed
only
I used
sed -i 's/ahmed.mohamed/mohamed.ahmed/g'
but make all like this I want to keep the character between them.
mohamed.ahmed
mohamed.ahmed
mohamed.ahmed
sed regular-expression
New contributor
add a comment |
up vote
1
down vote
favorite
How to use sed regex to replace to words related to each other and one character between them with out change the character and the two words as group
like that
ahmed#mohamed
ahmed$mohamed
ahmed7mohamed
I didn't want to replace ahmed
only and then replace mohamed
only
I used
sed -i 's/ahmed.mohamed/mohamed.ahmed/g'
but make all like this I want to keep the character between them.
mohamed.ahmed
mohamed.ahmed
mohamed.ahmed
sed regular-expression
New contributor
Thanks for showing us the result that you got from the command that you tried, but you should also show the result that you want. Your explanation of the result you wanted is pretty good, but not entirely clear.
– Scott
Nov 26 at 4:48
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
How to use sed regex to replace to words related to each other and one character between them with out change the character and the two words as group
like that
ahmed#mohamed
ahmed$mohamed
ahmed7mohamed
I didn't want to replace ahmed
only and then replace mohamed
only
I used
sed -i 's/ahmed.mohamed/mohamed.ahmed/g'
but make all like this I want to keep the character between them.
mohamed.ahmed
mohamed.ahmed
mohamed.ahmed
sed regular-expression
New contributor
How to use sed regex to replace to words related to each other and one character between them with out change the character and the two words as group
like that
ahmed#mohamed
ahmed$mohamed
ahmed7mohamed
I didn't want to replace ahmed
only and then replace mohamed
only
I used
sed -i 's/ahmed.mohamed/mohamed.ahmed/g'
but make all like this I want to keep the character between them.
mohamed.ahmed
mohamed.ahmed
mohamed.ahmed
sed regular-expression
sed regular-expression
New contributor
New contributor
edited Nov 26 at 7:54
Kusalananda
118k16221360
118k16221360
New contributor
asked Nov 26 at 3:20
Medo Gamal
82
82
New contributor
New contributor
Thanks for showing us the result that you got from the command that you tried, but you should also show the result that you want. Your explanation of the result you wanted is pretty good, but not entirely clear.
– Scott
Nov 26 at 4:48
add a comment |
Thanks for showing us the result that you got from the command that you tried, but you should also show the result that you want. Your explanation of the result you wanted is pretty good, but not entirely clear.
– Scott
Nov 26 at 4:48
Thanks for showing us the result that you got from the command that you tried, but you should also show the result that you want. Your explanation of the result you wanted is pretty good, but not entirely clear.
– Scott
Nov 26 at 4:48
Thanks for showing us the result that you got from the command that you tried, but you should also show the result that you want. Your explanation of the result you wanted is pretty good, but not entirely clear.
– Scott
Nov 26 at 4:48
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
You're very close. You need to use capturing parentheses:
sed -E -i 's/ahmed(.)mohamed/mohamed1ahmed/g'
The 1
is replaced with the text of the first set of parentheses.
Thank you very very much
– Medo Gamal
Nov 26 at 3:40
add a comment |
up vote
1
down vote
You want to swap the two strings ahmed
and mohamed
that are separated by some character.
The issue in your expression,
s/ahmed.mohamed/mohamed.ahmed/
is that the character in-between the words is always replaced by a dot. The solution is to capture the character and replace it with itself.
This is one way of doing so with sed
, which also uses the same capturing mechanism to avoid typing in the two strings again for the replacement:
sed 's/(ahmed)(.)(mohamed)/321/'
or,
sed -E 's/(ahmed)(.)(mohamed)/321/'
Testing on the given data:
$ sed -E 's/(ahmed)(.)(mohamed)/321/' <file
mohamed#ahmed
mohamed$ahmed
mohamed7ahmed
right Thank you very much for your help .
– Medo Gamal
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You're very close. You need to use capturing parentheses:
sed -E -i 's/ahmed(.)mohamed/mohamed1ahmed/g'
The 1
is replaced with the text of the first set of parentheses.
Thank you very very much
– Medo Gamal
Nov 26 at 3:40
add a comment |
up vote
1
down vote
accepted
You're very close. You need to use capturing parentheses:
sed -E -i 's/ahmed(.)mohamed/mohamed1ahmed/g'
The 1
is replaced with the text of the first set of parentheses.
Thank you very very much
– Medo Gamal
Nov 26 at 3:40
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You're very close. You need to use capturing parentheses:
sed -E -i 's/ahmed(.)mohamed/mohamed1ahmed/g'
The 1
is replaced with the text of the first set of parentheses.
You're very close. You need to use capturing parentheses:
sed -E -i 's/ahmed(.)mohamed/mohamed1ahmed/g'
The 1
is replaced with the text of the first set of parentheses.
answered Nov 26 at 3:35
glenn jackman
49.5k469106
49.5k469106
Thank you very very much
– Medo Gamal
Nov 26 at 3:40
add a comment |
Thank you very very much
– Medo Gamal
Nov 26 at 3:40
Thank you very very much
– Medo Gamal
Nov 26 at 3:40
Thank you very very much
– Medo Gamal
Nov 26 at 3:40
add a comment |
up vote
1
down vote
You want to swap the two strings ahmed
and mohamed
that are separated by some character.
The issue in your expression,
s/ahmed.mohamed/mohamed.ahmed/
is that the character in-between the words is always replaced by a dot. The solution is to capture the character and replace it with itself.
This is one way of doing so with sed
, which also uses the same capturing mechanism to avoid typing in the two strings again for the replacement:
sed 's/(ahmed)(.)(mohamed)/321/'
or,
sed -E 's/(ahmed)(.)(mohamed)/321/'
Testing on the given data:
$ sed -E 's/(ahmed)(.)(mohamed)/321/' <file
mohamed#ahmed
mohamed$ahmed
mohamed7ahmed
right Thank you very much for your help .
– Medo Gamal
2 days ago
add a comment |
up vote
1
down vote
You want to swap the two strings ahmed
and mohamed
that are separated by some character.
The issue in your expression,
s/ahmed.mohamed/mohamed.ahmed/
is that the character in-between the words is always replaced by a dot. The solution is to capture the character and replace it with itself.
This is one way of doing so with sed
, which also uses the same capturing mechanism to avoid typing in the two strings again for the replacement:
sed 's/(ahmed)(.)(mohamed)/321/'
or,
sed -E 's/(ahmed)(.)(mohamed)/321/'
Testing on the given data:
$ sed -E 's/(ahmed)(.)(mohamed)/321/' <file
mohamed#ahmed
mohamed$ahmed
mohamed7ahmed
right Thank you very much for your help .
– Medo Gamal
2 days ago
add a comment |
up vote
1
down vote
up vote
1
down vote
You want to swap the two strings ahmed
and mohamed
that are separated by some character.
The issue in your expression,
s/ahmed.mohamed/mohamed.ahmed/
is that the character in-between the words is always replaced by a dot. The solution is to capture the character and replace it with itself.
This is one way of doing so with sed
, which also uses the same capturing mechanism to avoid typing in the two strings again for the replacement:
sed 's/(ahmed)(.)(mohamed)/321/'
or,
sed -E 's/(ahmed)(.)(mohamed)/321/'
Testing on the given data:
$ sed -E 's/(ahmed)(.)(mohamed)/321/' <file
mohamed#ahmed
mohamed$ahmed
mohamed7ahmed
You want to swap the two strings ahmed
and mohamed
that are separated by some character.
The issue in your expression,
s/ahmed.mohamed/mohamed.ahmed/
is that the character in-between the words is always replaced by a dot. The solution is to capture the character and replace it with itself.
This is one way of doing so with sed
, which also uses the same capturing mechanism to avoid typing in the two strings again for the replacement:
sed 's/(ahmed)(.)(mohamed)/321/'
or,
sed -E 's/(ahmed)(.)(mohamed)/321/'
Testing on the given data:
$ sed -E 's/(ahmed)(.)(mohamed)/321/' <file
mohamed#ahmed
mohamed$ahmed
mohamed7ahmed
edited 2 days ago
answered Nov 26 at 8:05
Kusalananda
118k16221360
118k16221360
right Thank you very much for your help .
– Medo Gamal
2 days ago
add a comment |
right Thank you very much for your help .
– Medo Gamal
2 days ago
right Thank you very much for your help .
– Medo Gamal
2 days ago
right Thank you very much for your help .
– Medo Gamal
2 days ago
add a comment |
Medo Gamal is a new contributor. Be nice, and check out our Code of Conduct.
Medo Gamal is a new contributor. Be nice, and check out our Code of Conduct.
Medo Gamal is a new contributor. Be nice, and check out our Code of Conduct.
Medo Gamal is a new contributor. Be nice, and check out our Code of Conduct.
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%2f484124%2fhow-to-use-sed-regex-to-replace-to-words-related-to-each-other-and-one-character%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
Thanks for showing us the result that you got from the command that you tried, but you should also show the result that you want. Your explanation of the result you wanted is pretty good, but not entirely clear.
– Scott
Nov 26 at 4:48