Sponsored Content
Full Discussion: Random Line
Top Forums Shell Programming and Scripting Random Line Post 31879 by Vishnu on Thursday 14th of November 2002 11:24:14 AM
Old 11-14-2002
Thanks to Perderabo and Crashburn for this idea...

Code:
nlines=`wc -l filename`
VAR=`expr $RANDOM % $nlines + 1`

we need to add 1 so that we genrate line numbers between 1 to $nlines and not 0 to $nlines -1

or this should also work... here I'm ensuring by dividing with 32768 that the "random" number generated will fall within the no of lines that file contains.

On hp-ux 10, 11 we have man ksh giving

0 <= $RANDOM <= 32767

Code:
nlines=`wc -l filename`
VAR=`expr \( 1 + $RANDOM \) \* $nlines / 32768`

you can see the range for RANDOM by referring "man ksh" for your implementation...

Cheers!
Vishnu.

Last edited by Vishnu; 11-14-2002 at 12:37 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Random

My problem is as follow and i hope you can help: I currently have this function: stored_word() { number=$RANDOM let "number %= 21" case $number in 0 ) echo "energy" ;; 1 ) echo "touch" ;; 2 ) echo "climbing" ;; 3 ) echo "declare" ;; 4 ) echo "marry" ;; 5 ) echo "relax" ... (8 Replies)
Discussion started by: keyvan
8 Replies

2. Shell Programming and Scripting

$random

I need to use the $RANDOM command to get a line from a list of lines in a file randomly. file is help go three house film how do i randomly get one word without looking into the file? (6 Replies)
Discussion started by: relle
6 Replies

3. Shell Programming and Scripting

Read random line from a text file

I have a text file with hundreds of lines, i wish to run a script and reads a random line to pass it to another command line such as: for line in `cat file |grep random line`; do echo $line |mail my@example.com ; done thank you (6 Replies)
Discussion started by: Bashar
6 Replies

4. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

5. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

6. Shell Programming and Scripting

Parse large file on line count (random lines)

I have a file that needs to be parsed into multiple files every time there line contains a number 1. the problem i face is the lines are random and the file size is random. an example is that on line 4, 65, 187, 202 & 209 are number 1's so there has to be file breaks between all those to create 4... (6 Replies)
Discussion started by: darbs121
6 Replies

7. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

8. UNIX for Dummies Questions & Answers

Changing a special line and Byte in a random file

Hello I created 3 files by: dd if=/dev/urandom bs=1024 count=1000000 of=./testfile1 dd if=/dev/urandom bs=1024 count=5000000 of=./testfile2 dd if=/dev/urandom bs=1024 count=10000000 of=./testfile3 Now I want to know how to make a change in a specific byte and/or line of theses files? (2 Replies)
Discussion started by: frhling
2 Replies

9. Shell Programming and Scripting

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…… …… …… Please use CODE tags when... (8 Replies)
Discussion started by: hhdzhu
8 Replies

10. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies
curs_window(3CURSES)					     Curses Library Functions					      curs_window(3CURSES)

NAME
curs_window, newwin, delwin, mvwin, subwin, derwin, mvderwin, dupwin, wsyncup, syncok, wcursyncup, wsyncdown - create curses windows SYNOPSIS
cc [ flag ... ] file ... -lcurses [ library ... ] #include <curses.h> WINDOW *newwin(int nlines, int ncols, int begin_y, int begin_x); int delwin(WINDOW *win); int mvwin(WINDOW *win, int y, int x); WINDOW *subwin(WINDOW *orig, int nlines, int ncols, int begin_y, int begin_x); WINDOW *derwin(WINDOW *orig, int nlines, int ncols, int begin_y, int begin_x); int mvderwin(WINDOW *win, int par_y, int par_x); WINDOW *dupwin(WINDOW *win); void wsyncup(WINDOW *win); int syncok(WINDOW *win, bool bf); void wcursyncup(WINDOW *win); void wsyncdown(WINDOW *win); DESCRIPTION
The newwin() routine creates and returns a pointer to a new window with the given number of lines, nlines, and columns, ncols. The upper left-hand corner of the window is at line begin_y, column begin_x. If either nlines or ncols is zero, they default to LINES -- begin_y and COLS -- begin_x. A new full-screen window is created by calling newwin(0,0,0,0). The delwin() routine deletes the named window, freeing all memory associated with it. Subwindows must be deleted before the main window can be deleted. The mvwin() routine moves the window so that the upper left-hand corner is at position (x, y). If the move would cause the window to be off the screen, it is an error and the window is not moved. Moving subwindows is allowed, but should be avoided. The subwin() routine creates and returns a pointer to a new window with the given number of lines, nlines, and columns, ncols. The window is at position (begin_y, begin_x) on the screen. (This position is relative to the screen, and not to the window orig.) The window is made in the middle of the window orig, so that changes made to one window will affect both windows. The subwindow shares memory with the window orig. When using this routine, it is necessary to call touchwin() or touchline() on orig before calling wrefresh() on the subwindow. The derwin() routine is the same as subwin(), except that begin_y and begin_x are relative to the origin of the window orig rather than the screen. There is no difference between the subwindows and the derived windows. The mvderwin() routine moves a derived window (or subwindow) inside its parent window. The screen-relative parameters of the window are not changed. This routine is used to display different parts of the parent window at the same physical position on the screen. The dupwin() routine creates an exact duplicate of the window win. Each curses window maintains two data structures: the character image structure and the status structure. The character image structure is shared among all windows in the window hierarchy (that is, the window with all subwindows). The status structure, which contains informa- tion about individual line changes in the window, is private to each window. The routine wrefresh() uses the status data structure when performing screen updating. Since status structures are not shared, changes made to one window in the hierarchy may not be properly reflected on the screen. The routine wsyncup() causes the changes in the status structure of a window to be reflected in the status structures of its ancestors. If syncok() is called with second argument TRUE then wsyncup() is called automatically whenever there is a change in the window. The routine wcursyncup() updates the current cursor position of all the ancestors of the window to reflect the current cursor position of the window. The routine wsyncdown() updates the status structure of the window to reflect the changes in the status structures of its ancestors. Appli- cations seldom call this routine because it is called automatically by wrefresh(). RETURN VALUES
Routines that return an integer return the integer ERR upon failure and an integer value other than ERR upon successful completion. delwin() returns the integer ERR upon failure and OK upon successful completion. Routines that return pointers return NULL on error. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
curs_refresh(3CURSES), curs_touch(3CURSES), curses(3CURSES), attributes(5) NOTES
The header <curses.h> automatically includes the headers <stdio.h> and <unctrl.h>. If many small changes are made to the window, the wsyncup() option could degrade performance. Note that syncok() may be a macro. SunOS 5.11 31 Dec 1996 curs_window(3CURSES)
All times are GMT -4. The time now is 01:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy