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









share|improve this question









New contributor




Medo Gamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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















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









share|improve this question









New contributor




Medo Gamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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













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









share|improve this question









New contributor




Medo Gamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Medo Gamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Medo Gamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 26 at 7:54









Kusalananda

118k16221360




118k16221360






New contributor




Medo Gamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 26 at 3:20









Medo Gamal

82




82




New contributor




Medo Gamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Medo Gamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Medo Gamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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




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










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.






share|improve this answer





















  • Thank you very very much
    – Medo Gamal
    Nov 26 at 3:40


















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





share|improve this answer























  • right Thank you very much for your help .
    – Medo Gamal
    2 days ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Medo Gamal is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















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

























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.






share|improve this answer





















  • Thank you very very much
    – Medo Gamal
    Nov 26 at 3:40















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.






share|improve this answer





















  • Thank you very very much
    – Medo Gamal
    Nov 26 at 3:40













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 at 3:35









glenn jackman

49.5k469106




49.5k469106












  • 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




Thank you very very much
– Medo Gamal
Nov 26 at 3:40












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





share|improve this answer























  • right Thank you very much for your help .
    – Medo Gamal
    2 days ago















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





share|improve this answer























  • right Thank you very much for your help .
    – Medo Gamal
    2 days ago













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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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










Medo Gamal is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

サソリ

広島県道265号伴広島線

Setup Asymptote in Texstudio