I have a file contains thousands of lines. I want to insert n random numbers into each line at
specific position. Like this:
I want to insert 3 random numbers from position 2 to 4 into each line.
Moderator's Comments:
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.
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
, i.e. line contains somthing like
(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
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.
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
, i.e. line contains somthing like
(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
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).
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:
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:
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
instead.....
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)
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)
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)
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)
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)
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)
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)
Is anyone know some scripts to generate random number without repetition using bash; for example generate 10 different random numbers.
Thanks (8 Replies)