sed command that replaces number and word by two

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












1) sed command which puts two of the first 'w' on each line. E.g "hewor" = "hewwor".



and



2) sed command which puts two of the first digit on each line E.g "hew0r" = "hew00r"



For first one I got



$ sed s/w/ww/ 


for the second one I don't understand how to replicate the same digit for example I got



$ sed s/[0-9]/00/


would work but it would have to be zero each time. How do I get the same digit?







share|improve this question


















  • 1




    How about reading the manual... You'll learn how to reference the matched portion, regardless of what's in the LHS. ;)
    – don_crissti
    Jan 22 at 0:13










  • My bad, I meant $sed s/[0-9]/00/ as you can see, it has to be zero. I'm looking for a way for the digit to be the one found in the [0-9].
    – Tinler
    Jan 22 at 0:20










  • "sed s/[0-9]//" does the exact same as "sed s/[0-9]/00/" or am I missing something?
    – Tinler
    Jan 22 at 0:42










  • Yes. It becomes hew4r -> hew00r. I want hew44r. Also, why should I use that instead of "sed s/[0-9]/00/"? For this and the first example?
    – Tinler
    Jan 22 at 0:51










  • OK. in gnu sed using refers to the previously matched regex. In bsd sed this is done using &. So in your case since does not work, you can use sed 's/[0-9]/&&/'. Actuall using & will work even in gnu sed.
    – George Vasiliou
    Jan 22 at 1:02















up vote
1
down vote

favorite












1) sed command which puts two of the first 'w' on each line. E.g "hewor" = "hewwor".



and



2) sed command which puts two of the first digit on each line E.g "hew0r" = "hew00r"



For first one I got



$ sed s/w/ww/ 


for the second one I don't understand how to replicate the same digit for example I got



$ sed s/[0-9]/00/


would work but it would have to be zero each time. How do I get the same digit?







share|improve this question


















  • 1




    How about reading the manual... You'll learn how to reference the matched portion, regardless of what's in the LHS. ;)
    – don_crissti
    Jan 22 at 0:13










  • My bad, I meant $sed s/[0-9]/00/ as you can see, it has to be zero. I'm looking for a way for the digit to be the one found in the [0-9].
    – Tinler
    Jan 22 at 0:20










  • "sed s/[0-9]//" does the exact same as "sed s/[0-9]/00/" or am I missing something?
    – Tinler
    Jan 22 at 0:42










  • Yes. It becomes hew4r -> hew00r. I want hew44r. Also, why should I use that instead of "sed s/[0-9]/00/"? For this and the first example?
    – Tinler
    Jan 22 at 0:51










  • OK. in gnu sed using refers to the previously matched regex. In bsd sed this is done using &. So in your case since does not work, you can use sed 's/[0-9]/&&/'. Actuall using & will work even in gnu sed.
    – George Vasiliou
    Jan 22 at 1:02













up vote
1
down vote

favorite









up vote
1
down vote

favorite











1) sed command which puts two of the first 'w' on each line. E.g "hewor" = "hewwor".



and



2) sed command which puts two of the first digit on each line E.g "hew0r" = "hew00r"



For first one I got



$ sed s/w/ww/ 


for the second one I don't understand how to replicate the same digit for example I got



$ sed s/[0-9]/00/


would work but it would have to be zero each time. How do I get the same digit?







share|improve this question














1) sed command which puts two of the first 'w' on each line. E.g "hewor" = "hewwor".



and



2) sed command which puts two of the first digit on each line E.g "hew0r" = "hew00r"



For first one I got



$ sed s/w/ww/ 


for the second one I don't understand how to replicate the same digit for example I got



$ sed s/[0-9]/00/


would work but it would have to be zero each time. How do I get the same digit?









share|improve this question













share|improve this question




share|improve this question








edited Jan 22 at 0:20

























asked Jan 22 at 0:08









Tinler

1295




1295







  • 1




    How about reading the manual... You'll learn how to reference the matched portion, regardless of what's in the LHS. ;)
    – don_crissti
    Jan 22 at 0:13










  • My bad, I meant $sed s/[0-9]/00/ as you can see, it has to be zero. I'm looking for a way for the digit to be the one found in the [0-9].
    – Tinler
    Jan 22 at 0:20










  • "sed s/[0-9]//" does the exact same as "sed s/[0-9]/00/" or am I missing something?
    – Tinler
    Jan 22 at 0:42










  • Yes. It becomes hew4r -> hew00r. I want hew44r. Also, why should I use that instead of "sed s/[0-9]/00/"? For this and the first example?
    – Tinler
    Jan 22 at 0:51










  • OK. in gnu sed using refers to the previously matched regex. In bsd sed this is done using &. So in your case since does not work, you can use sed 's/[0-9]/&&/'. Actuall using & will work even in gnu sed.
    – George Vasiliou
    Jan 22 at 1:02













  • 1




    How about reading the manual... You'll learn how to reference the matched portion, regardless of what's in the LHS. ;)
    – don_crissti
    Jan 22 at 0:13










  • My bad, I meant $sed s/[0-9]/00/ as you can see, it has to be zero. I'm looking for a way for the digit to be the one found in the [0-9].
    – Tinler
    Jan 22 at 0:20










  • "sed s/[0-9]//" does the exact same as "sed s/[0-9]/00/" or am I missing something?
    – Tinler
    Jan 22 at 0:42










  • Yes. It becomes hew4r -> hew00r. I want hew44r. Also, why should I use that instead of "sed s/[0-9]/00/"? For this and the first example?
    – Tinler
    Jan 22 at 0:51










  • OK. in gnu sed using refers to the previously matched regex. In bsd sed this is done using &. So in your case since does not work, you can use sed 's/[0-9]/&&/'. Actuall using & will work even in gnu sed.
    – George Vasiliou
    Jan 22 at 1:02








1




1




How about reading the manual... You'll learn how to reference the matched portion, regardless of what's in the LHS. ;)
– don_crissti
Jan 22 at 0:13




How about reading the manual... You'll learn how to reference the matched portion, regardless of what's in the LHS. ;)
– don_crissti
Jan 22 at 0:13












My bad, I meant $sed s/[0-9]/00/ as you can see, it has to be zero. I'm looking for a way for the digit to be the one found in the [0-9].
– Tinler
Jan 22 at 0:20




My bad, I meant $sed s/[0-9]/00/ as you can see, it has to be zero. I'm looking for a way for the digit to be the one found in the [0-9].
– Tinler
Jan 22 at 0:20












"sed s/[0-9]//" does the exact same as "sed s/[0-9]/00/" or am I missing something?
– Tinler
Jan 22 at 0:42




"sed s/[0-9]//" does the exact same as "sed s/[0-9]/00/" or am I missing something?
– Tinler
Jan 22 at 0:42












Yes. It becomes hew4r -> hew00r. I want hew44r. Also, why should I use that instead of "sed s/[0-9]/00/"? For this and the first example?
– Tinler
Jan 22 at 0:51




Yes. It becomes hew4r -> hew00r. I want hew44r. Also, why should I use that instead of "sed s/[0-9]/00/"? For this and the first example?
– Tinler
Jan 22 at 0:51












OK. in gnu sed using refers to the previously matched regex. In bsd sed this is done using &. So in your case since does not work, you can use sed 's/[0-9]/&&/'. Actuall using & will work even in gnu sed.
– George Vasiliou
Jan 22 at 1:02





OK. in gnu sed using refers to the previously matched regex. In bsd sed this is done using &. So in your case since does not work, you can use sed 's/[0-9]/&&/'. Actuall using & will work even in gnu sed.
– George Vasiliou
Jan 22 at 1:02











1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You need to use the sed's feature, called groups (may be not the best reference, try to search for other tutorials). In you case the solution is



sed 's/([0-9])/11/' input_file.txt


the regexp for the first group ([0-9]) will match any digit, and the part 11 says to replace the first group with itself repeated twice.






share|improve this answer
















  • 1




    I don't think the purpose of this assignment is to teach them how to use groups. The link is good though, it has the answer.
    – don_crissti
    Jan 22 at 0:29






  • 2




    A lot of people helped me here answering my questions when I just began. I am just returning this help.
    – John Smith
    Jan 22 at 0:34










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: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f418714%2fsed-command-that-replaces-number-and-word-by-two%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










You need to use the sed's feature, called groups (may be not the best reference, try to search for other tutorials). In you case the solution is



sed 's/([0-9])/11/' input_file.txt


the regexp for the first group ([0-9]) will match any digit, and the part 11 says to replace the first group with itself repeated twice.






share|improve this answer
















  • 1




    I don't think the purpose of this assignment is to teach them how to use groups. The link is good though, it has the answer.
    – don_crissti
    Jan 22 at 0:29






  • 2




    A lot of people helped me here answering my questions when I just began. I am just returning this help.
    – John Smith
    Jan 22 at 0:34














up vote
2
down vote



accepted










You need to use the sed's feature, called groups (may be not the best reference, try to search for other tutorials). In you case the solution is



sed 's/([0-9])/11/' input_file.txt


the regexp for the first group ([0-9]) will match any digit, and the part 11 says to replace the first group with itself repeated twice.






share|improve this answer
















  • 1




    I don't think the purpose of this assignment is to teach them how to use groups. The link is good though, it has the answer.
    – don_crissti
    Jan 22 at 0:29






  • 2




    A lot of people helped me here answering my questions when I just began. I am just returning this help.
    – John Smith
    Jan 22 at 0:34












up vote
2
down vote



accepted







up vote
2
down vote



accepted






You need to use the sed's feature, called groups (may be not the best reference, try to search for other tutorials). In you case the solution is



sed 's/([0-9])/11/' input_file.txt


the regexp for the first group ([0-9]) will match any digit, and the part 11 says to replace the first group with itself repeated twice.






share|improve this answer












You need to use the sed's feature, called groups (may be not the best reference, try to search for other tutorials). In you case the solution is



sed 's/([0-9])/11/' input_file.txt


the regexp for the first group ([0-9]) will match any digit, and the part 11 says to replace the first group with itself repeated twice.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 22 at 0:25









John Smith

95857




95857







  • 1




    I don't think the purpose of this assignment is to teach them how to use groups. The link is good though, it has the answer.
    – don_crissti
    Jan 22 at 0:29






  • 2




    A lot of people helped me here answering my questions when I just began. I am just returning this help.
    – John Smith
    Jan 22 at 0:34












  • 1




    I don't think the purpose of this assignment is to teach them how to use groups. The link is good though, it has the answer.
    – don_crissti
    Jan 22 at 0:29






  • 2




    A lot of people helped me here answering my questions when I just began. I am just returning this help.
    – John Smith
    Jan 22 at 0:34







1




1




I don't think the purpose of this assignment is to teach them how to use groups. The link is good though, it has the answer.
– don_crissti
Jan 22 at 0:29




I don't think the purpose of this assignment is to teach them how to use groups. The link is good though, it has the answer.
– don_crissti
Jan 22 at 0:29




2




2




A lot of people helped me here answering my questions when I just began. I am just returning this help.
– John Smith
Jan 22 at 0:34




A lot of people helped me here answering my questions when I just began. I am just returning this help.
– John Smith
Jan 22 at 0:34












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f418714%2fsed-command-that-replaces-number-and-word-by-two%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay