How to insert random numbers into each line?

I have a file contains thousands of lines. I want to insert n random numbers into each line at
specific position. Like this:

0      22��
1      33��
��
��

I want to insert 3 random numbers from position 2 to 4 into each line.

0  325  22��
1  685  33��
��
��

I don't understand your requirements.

How does changing two spaces that occupy the 4th and 5th characters on an input line into 3 digits meet the requirement to "insert 3 random numbers from position 2 to 4 into each line"?

As Don said, your example does not match the specification you gave. Maybe you didn't mean "insert 3 random numbers", but "replace the spaces (at a certain position) by a 3-digit random number"?

---------- Post updated at 08:18 AM ---------- Previous update was at 08:09 AM ----------

You did not say, what shell you want to use. I give you an example solution in Zsh (which is pretty convenient when it comes to programming):

Say you process your file line by line, and have stored one line in variable

line

, i.e. line contains somthing like

4     44

(i.e. 5 spaces between the numbers), and you want to replace the middle three spaces by a random number. This could be done in Zsh by

echo ${line:s/    / $((RANDOM%1000))}

Note that there are 4 spaces between the pair of slashes, and exactly one space after the second slash. Hence, the expression substitutes the first four spaces in the line, by a single space followed by a 3-digit random number.

1 Like

Except, of course, that you'll get a 1-digit random number about 10 times out of a thousand (replacing 4 spaces with 2 characters) and a 2-digit random number about 90 times out of a thousand (replacing 4 spaces with 3 characters).

my fault,sorry! offset 2 to 4 was reserved by fill with spaces at first. now, i want to use this position by replace spaces with
some random numbers for testing! thank you very much!

Changing the 4th and 5th characters on your input lines to 3-digit pseudo-random numbers in the output (as shown in your sample input and output) could be done with something like:

awk '
BEGIN {	srand()
}
{	printf("%s%03d%s\n", substr($0, 1, 3), rand() * 1000, substr($0, 6))
}' file

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

You can adjust the number and positions of the characters replaced in your input lines to match the description of your requirements instead of matching the sample input and output you provided if you want to.

1 Like

Ooops. You are right (and that's trivial to fix). Unless we are happy with leading zeroes in the random number, we should use

$((100+RANDOM%900))

instead.....

Hi roof,
Why don't you think 000 and 029 just as valid as 3-digit random numbers as 100 and 999?

Assuming that characters two through four on every input line are spaces (which was not stated in the requirements, but is true in the given 2-line sample input) and you're using a recent bash , a 1993 or later ksh , or zsh , you could change your suggestion from:

echo ${line:s/    / $((RANDOM%1000))}

to:

echo ${line:s/    / $((RANDOM%10))$((RANDOM%10))$((RANDOM%10))}

to change characters three through five to a 3-digit random number in the given sample input.

But, hhdzhu still hasn't given us a clear specification of what output is desired. (Specifying offsets 2 to 4 are to be changed is ambiguous since we haven't been told whether the 1st character on a line is offset 0 (as in bash and recent ksh ${variable:offset[:length]} ) or offset 1 (as in awk substr(string, offset[, length]) ) and sample input and output that does' t match either of these.

And, since the input is "thousands of lines", it isn't obvious whether or not my awk suggestion would be slower or faster processing the desired file than the zsh (or ksh93 or bash ) script you suggested (modified as shown above) inserted into a while read loop to process the entire file instead of just one line.

I didn't say it isn't, but it might be. Some people consider a 3-digit number as one which is higher than 99 (and might be disappointed, if they get promised an hourly payment in the "range of a 3-digit-number" and end up getting payed US$ 002 per hour), and others are happy with a leading zero. The OP didn't say anything, and I gave my implementation as an example.

To be honest, even for a couple of thousands of lines, speed difference should only matter if the same type of transformation is done many times in a loop. I gave zsh as an example, because I personally use either zsh or Ruby for this type of task (out of a habit), but bash, Perl, Python, awk etc. work of course equally well, and the OP did not have expressed a preference for a particular language.