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

Clash 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
command-line text-processing sed
add a comment |Â
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
command-line text-processing sed
this stackoverflow.com/questions/1194882/⦠could help
â Lety
Mar 21 at 17:39
add a comment |Â
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
command-line text-processing sed
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
command-line text-processing sed
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
add a comment |Â
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
add a comment |Â
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.
It worked,Thanks a lot :)
â Deepak
Mar 22 at 2: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
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.
It worked,Thanks a lot :)
â Deepak
Mar 22 at 2:17
add a comment |Â
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.
It worked,Thanks a lot :)
â Deepak
Mar 22 at 2:17
add a comment |Â
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.
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.
answered Mar 21 at 17:58
steeldriver
63.1k1198166
63.1k1198166
It worked,Thanks a lot :)
â Deepak
Mar 22 at 2:17
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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
this stackoverflow.com/questions/1194882/⦠could help
â Lety
Mar 21 at 17:39