Sponsored Content
Top Forums Shell Programming and Scripting How to insert random numbers into each line? Post 302973917 by Don Cragun on Tuesday 24th of May 2016 02:43:32 AM
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).
 

8 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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
zlarnv.f(3)							      LAPACK							       zlarnv.f(3)

NAME
zlarnv.f - SYNOPSIS
Functions/Subroutines subroutine zlarnv (IDIST, ISEED, N, X) ZLARNV Function/Subroutine Documentation subroutine zlarnv (integerIDIST, integer, dimension( 4 )ISEED, integerN, complex*16, dimension( * )X) ZLARNV Purpose: ZLARNV returns a vector of n random complex numbers from a uniform or normal distribution. Parameters: IDIST IDIST is INTEGER Specifies the distribution of the random numbers: = 1: real and imaginary parts each uniform (0,1) = 2: real and imaginary parts each uniform (-1,1) = 3: real and imaginary parts each normal (0,1) = 4: uniformly distributed on the disc abs(z) < 1 = 5: uniformly distributed on the circle abs(z) = 1 ISEED ISEED is INTEGER array, dimension (4) On entry, the seed of the random number generator; the array elements must be between 0 and 4095, and ISEED(4) must be odd. On exit, the seed is updated. N N is INTEGER The number of random numbers to be generated. X X is COMPLEX*16 array, dimension (N) The generated random numbers. Author: Univ. of Tennessee Univ. of California Berkeley Univ. of Colorado Denver NAG Ltd. Date: November 2011 Further Details: This routine calls the auxiliary routine DLARUV to generate random real numbers from a uniform (0,1) distribution, in batches of up to 128 using vectorisable code. The Box-Muller method is used to transform numbers from a uniform to a normal distribution. Definition at line 100 of file zlarnv.f. Author Generated automatically by Doxygen for LAPACK from the source code. Version 3.4.1 Sun May 26 2013 zlarnv.f(3)
All times are GMT -4. The time now is 12:47 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy