How to use sed to modify last 3 digit of a line with random number when a string matches in a file

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








up vote
0
down vote

favorite












I have a file 'a.txt' which have a content:



K8A2-19C204-BA109691 
ML_18.10.4-109691


How can i search for a string say "ML_" in the file and if it matches modify last 3 digit of that line with some random numbers using 'sed' command in bash script.

I am able to change the last 3 numbers to some fixed numbers with the command:



sed -i '/ML_/s/[0-9][0-9][0-9]$/012/' file_name 


But not sure how to change with random numbers










share|improve this question























  • this stackoverflow.com/questions/1194882/… could help
    – Lety
    Mar 21 at 17:39














up vote
0
down vote

favorite












I have a file 'a.txt' which have a content:



K8A2-19C204-BA109691 
ML_18.10.4-109691


How can i search for a string say "ML_" in the file and if it matches modify last 3 digit of that line with some random numbers using 'sed' command in bash script.

I am able to change the last 3 numbers to some fixed numbers with the command:



sed -i '/ML_/s/[0-9][0-9][0-9]$/012/' file_name 


But not sure how to change with random numbers










share|improve this question























  • this stackoverflow.com/questions/1194882/… could help
    – Lety
    Mar 21 at 17:39












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a file 'a.txt' which have a content:



K8A2-19C204-BA109691 
ML_18.10.4-109691


How can i search for a string say "ML_" in the file and if it matches modify last 3 digit of that line with some random numbers using 'sed' command in bash script.

I am able to change the last 3 numbers to some fixed numbers with the command:



sed -i '/ML_/s/[0-9][0-9][0-9]$/012/' file_name 


But not sure how to change with random numbers










share|improve this question















I have a file 'a.txt' which have a content:



K8A2-19C204-BA109691 
ML_18.10.4-109691


How can i search for a string say "ML_" in the file and if it matches modify last 3 digit of that line with some random numbers using 'sed' command in bash script.

I am able to change the last 3 numbers to some fixed numbers with the command:



sed -i '/ML_/s/[0-9][0-9][0-9]$/012/' file_name 


But not sure how to change with random numbers







command-line text-processing sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 1:54









muru

130k19273463




130k19273463










asked Mar 21 at 17:24









Deepak

31




31











  • this stackoverflow.com/questions/1194882/… could help
    – Lety
    Mar 21 at 17:39
















  • this stackoverflow.com/questions/1194882/… could help
    – Lety
    Mar 21 at 17:39















this stackoverflow.com/questions/1194882/… could help
– Lety
Mar 21 at 17:39




this stackoverflow.com/questions/1194882/… could help
– Lety
Mar 21 at 17:39










1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










If you don't have any particular requirements for the randomness, then you can use the bash shell's built in RANDOM as mentioned in comments e.g.



$ sed '/^ML_/ s/[0-9]3$/'$(printf '%03d' $((RANDOM%1000)))'/' a.txt 
K8A2-19C204-BA109691
ML_18.10.4-109133


If you require "better" random numbers, then I'd suggest abandoning sed in favor of perl and looking for a suitable random number module.






share|improve this answer




















  • It worked,Thanks a lot :)
    – Deepak
    Mar 22 at 2:17










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
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: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2faskubuntu.com%2fquestions%2f1018011%2fhow-to-use-sed-to-modify-last-3-digit-of-a-line-with-random-number-when-a-string%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
0
down vote



accepted










If you don't have any particular requirements for the randomness, then you can use the bash shell's built in RANDOM as mentioned in comments e.g.



$ sed '/^ML_/ s/[0-9]3$/'$(printf '%03d' $((RANDOM%1000)))'/' a.txt 
K8A2-19C204-BA109691
ML_18.10.4-109133


If you require "better" random numbers, then I'd suggest abandoning sed in favor of perl and looking for a suitable random number module.






share|improve this answer




















  • It worked,Thanks a lot :)
    – Deepak
    Mar 22 at 2:17














up vote
0
down vote



accepted










If you don't have any particular requirements for the randomness, then you can use the bash shell's built in RANDOM as mentioned in comments e.g.



$ sed '/^ML_/ s/[0-9]3$/'$(printf '%03d' $((RANDOM%1000)))'/' a.txt 
K8A2-19C204-BA109691
ML_18.10.4-109133


If you require "better" random numbers, then I'd suggest abandoning sed in favor of perl and looking for a suitable random number module.






share|improve this answer




















  • It worked,Thanks a lot :)
    – Deepak
    Mar 22 at 2:17












up vote
0
down vote



accepted







up vote
0
down vote



accepted






If you don't have any particular requirements for the randomness, then you can use the bash shell's built in RANDOM as mentioned in comments e.g.



$ sed '/^ML_/ s/[0-9]3$/'$(printf '%03d' $((RANDOM%1000)))'/' a.txt 
K8A2-19C204-BA109691
ML_18.10.4-109133


If you require "better" random numbers, then I'd suggest abandoning sed in favor of perl and looking for a suitable random number module.






share|improve this answer












If you don't have any particular requirements for the randomness, then you can use the bash shell's built in RANDOM as mentioned in comments e.g.



$ sed '/^ML_/ s/[0-9]3$/'$(printf '%03d' $((RANDOM%1000)))'/' a.txt 
K8A2-19C204-BA109691
ML_18.10.4-109133


If you require "better" random numbers, then I'd suggest abandoning sed in favor of perl and looking for a suitable random number module.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 21 at 17:58









steeldriver

63.1k1198166




63.1k1198166











  • It worked,Thanks a lot :)
    – Deepak
    Mar 22 at 2:17
















  • It worked,Thanks a lot :)
    – Deepak
    Mar 22 at 2:17















It worked,Thanks a lot :)
– Deepak
Mar 22 at 2:17




It worked,Thanks a lot :)
– Deepak
Mar 22 at 2:17

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1018011%2fhow-to-use-sed-to-modify-last-3-digit-of-a-line-with-random-number-when-a-string%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Which professions warranted travel in Medieval times?

How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

Trouble downloading packages list due to a “Hash sum mismatch” error