How to insert random numbers into each line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert random numbers into each line?
# 8  
Old 05-24-2016
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:
Code:
echo ${line:s/    / $((RANDOM%1000))}

to:
Code:
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.
# 9  
Old 05-24-2016
Quote:
Originally Posted by Don Cragun
Hi roof,
Why don't you think 000 and 029 just as valid as 3-digit random numbers as 100 and 999?
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.

Quote:
Originally Posted by Don Cragun
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.
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Random numbers

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! Write a shell script that will take the sum of two random number? Ex: Random n1 +Random n2 = result i tries to write it but i had some dufficulties ... (3 Replies)
Discussion started by: renegade755
3 Replies

2. Shell Programming and Scripting

insert one line inbetween file with use of numbers

Hi All, I have one file name called test.txt and its having numbers inside the file.I need to identified next available number with use of unix shell scipt and have to update next available number with use of unix shell script. Example:- cat test.txt 5001 5002 5003 5005 7000 7001 ... (5 Replies)
Discussion started by: susindram
5 Replies

3. Shell Programming and Scripting

Insert rows based on line numbers

Can I insert rows based on line numbers. Say If I need to insert 1 or more rows in a file from line number 10. Can I do that in UNIX I have a file something like A B C D E F After row C, I wanted to add 2 records as X and Y. I have the line number after C as my reference. Can I... (2 Replies)
Discussion started by: Muthuraj K
2 Replies

4. Shell Programming and Scripting

Replace a random string of numbers

Hi Can someone help me with this one? I have string.. (PROC_PROC_ID == 12183) <--PID is dynamic and i want to replace the PID number with whatever PID from /opt/hpws/apache32_2/logs/httpd.pid file. i'm having problem since the PID on the string is dynamic. It may be 2-5 digits or more. ... (5 Replies)
Discussion started by: ryandegreat25
5 Replies

5. Shell Programming and Scripting

Generating random numbers

Hi, I am having trouble with generating random numbers. can this be done with awk? So I have a file that looks like this: 23 30 24 40 26 34 So column1 is start and column2 is end. I want to generate 3 random #'s between start and stop: So the output will look like this: ... (9 Replies)
Discussion started by: phil_heath
9 Replies

6. Shell Programming and Scripting

Random Numbers - Perl

Hi Guys I have a script to find Ranomd numbers. But I want to make the file to produce more random. Could u guys help me plz. In this Script I have the code that generates random in for loop and the range I have specified in my %chromlength input and out put will be like this chrno start end... (3 Replies)
Discussion started by: repinementer
3 Replies

7. Shell Programming and Scripting

Random numbers from 0 to 1000

Hello All, I want to make a simple script which generate random number from 0 to 1000. and simply display it. Plz HELP!!!!!! Regards, Waqas Ahmed (2 Replies)
Discussion started by: wakhan
2 Replies

8. UNIX for Dummies Questions & Answers

Random numbers without repetition

Is anyone know some scripts to generate random number without repetition using bash; for example generate 10 different random numbers. Thanks (8 Replies)
Discussion started by: asal_email
8 Replies
Login or Register to Ask a Question