Visit Our UNIX and Linux User Community


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?
# 1  
Old 05-23-2016
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:
Code:
0      22……
1      33……
……
……

I want to insert 3 random numbers from position 2 to 4 into each line.
Code:
0  325  22……
1  685  33……
……
……

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments.

Last edited by Don Cragun; 05-23-2016 at 11:05 PM.. Reason: Add CODE tags.
# 2  
Old 05-24-2016
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"?
# 3  
Old 05-24-2016
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
Code:
line

, i.e. line contains somthing like
Code:
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

Code:
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.
This User Gave Thanks to rovf For This Post:
# 4  
Old 05-24-2016
Quote:
Originally Posted by rovf
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
Code:
line

, i.e. line contains somthing like
Code:
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

Code:
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.
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).
# 5  
Old 05-24-2016
Quote:
Originally Posted by Don Cragun
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!
# 6  
Old 05-24-2016
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:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 05-24-2016
Quote:
Originally Posted by Don Cragun
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).
Ooops. You are right (and that's trivial to fix). Unless we are happy with leading zeroes in the random number, we should use
Code:
$((100+RANDOM%900))

instead.....

Previous Thread | Next Thread
Test Your Knowledge in Computers #659
Difficulty: Medium
It is not possible to run DOS on Linux.
True or False?

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

Featured Tech Videos