Red_badger
Red_badger

Reputation: 139

Change the nth character to a Random character in Linux

i have a text file with a list of numbers. I would like to change the 11th character to a random number. input.txt
00018674395618674395
00018674396618674396
00018674397618674397
00018674398618674398
00018674399618674399
00018674400618674400

 sed 's/^\(.\{11\}\)./\1'$(((RANDOM % 10)+1))'/' input.txt > output.txt

However, this just generates a random number and inserts it as the 11th character, so the 11th number becomes for example 4 in every-line. I would like a random number in each line 00018674395618674395
00018674396318674396
00018674397518674397
00018674398718674398
00018674399718674399
00018674400218674400

thanks in advance

Upvotes: 0

Views: 96

Answers (1)

jas
jas

Reputation: 10865

srand() seeds the random number generator in case you want a different string of random numbers each time you generate the results. If not, you can remove all of BEGIN {srand()}. (The -*- is not really in the output, it's just to make it easier to see where the random digit is placed.)

$ awk 'BEGIN {srand()} {print substr($0,1,10) (int(10 * rand())) substr($0,12)}' r.txt
         -*-     
00018674398618674395
00018674394618674396
00018674395618674397
00018674391618674398
00018674394618674399
00018674405618674400
         -*-     

$ awk 'BEGIN {srand()} {print substr($0,1,10) (int(10 * rand())) substr($0,12)}' r.txt
         -*-     
00018674399618674395
00018674394618674396
00018674396618674397
00018674397618674398
00018674391618674399
00018674405618674400
         -*-

Upvotes: 1

Related Questions