0

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.