Sponsored Content
Top Forums UNIX for Dummies Questions & Answers How to randomly select lines from a text file Post 302720879 by alister on Wednesday 24th of October 2012 06:28:29 PM
Old 10-24-2012
Quote:
Originally Posted by Corona688
Code:
awk 'NR==FNR { B++; next }
(NR != FNR) && (!Z) {
        srand();
        if(Z < 200) exit(1); # Too few lines
        for(N=1; N<=200; N++)
        {
                V=sprintf("%d", (rand()*B)+1)+0;
                if(!(V in A)) { A[V]=1; N++ }
        }
        Z=1
} NR in A' inputfile inputfile

Looks like there are a couple of bugs there. The first, a typo of Z for B, always exits before selecting anything. The second, the for-loop post-increment expression, can cause the algorithm to yield anywhere between 1 and 200 lines.

Probably no need to fix it since Scrutinizer's first example in post #5 is a correct implementation of the same approach.

Regards,
Alister

---------- Post updated at 06:28 PM ---------- Previous update was at 06:26 PM ----------

With regard to all of the AWK suggestions, without knowing exactly how the script is to be used, it's possible that all of the recommendations are inadequate. Nearly every awk srand implementation's default seed is the number of seconds since the epoch. Successive or simultaneous runs could yield identical results. May or may not be an issue. We don't have sufficient information to make that determination. Just a head's up for the OP.

If it is an issue, more information would be required to determine a robust seed expression.

Regards,
Alister
This User Gave Thanks to alister For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to select a value randomly

on my desktop i am using the kde rotating desktop image option. this rotates images randomly every half hour. now, i would like to write an html file which will have an inline frame with some text, maybe system messages, or my friends live journal thati read alot, or unix.com! however, i dont want... (1 Reply)
Discussion started by: norsk hedensk
1 Replies

2. AIX

Randomly appearing control characters in text files

Hi, From some time, we have noticed that our ascii files have started corrupting due to the presence of some random control characters (^@, ^M, ^H, ^D). The characters appear randomly on any file after the process that creates the file finishes. If we rerun the process, the files re creates... (0 Replies)
Discussion started by: aakashahuja
0 Replies

3. UNIX for Dummies Questions & Answers

Select only certain lines from file and mantain formatting

I want to take the below data, and have it output to file only the STMC#/(IP address) and the "there are X number of updates to install" lines for each machine. I know it's easy, but Im a beginner in BASH stuff, my solution would probably take way too many lines to do something easy.Thanks! ... (5 Replies)
Discussion started by: glev2005
5 Replies

4. Shell Programming and Scripting

Select some lines from a txt file and create a new file with awk

Hi there, I have a text file with several colums separated by "|;#" I need to search the file extracting all columns starting with the value of "1" or "2" saving in a separate file just the first 7 columns of each row maching the criteria, with replacement of the saparators in the nearly created... (4 Replies)
Discussion started by: capnino
4 Replies

5. Shell Programming and Scripting

Select lines in which column have value greater than some percent of total file lines

i have a file in following format 1 32 3 4 6 4 4 45 1 45 4 61 54 66 4 5 65 51 56 65 1 12 32 85 now here the total number of lines are 8(they vary each time) Now i want to select only those lines in which the values... (6 Replies)
Discussion started by: vaibhavkorde
6 Replies

6. Shell Programming and Scripting

randomly shuffle two text files the same way

What I have are two text files that I need to shuffle randomly, but I need the two files to be randomly shuffled the same way. I have heard of shuf but I do not know how to use it for two files. Maybe there is also an easy/simple awk command I do not know about that could handle this problem. ... (3 Replies)
Discussion started by: adrunknarwhal
3 Replies

7. Shell Programming and Scripting

Get 20% of lines in File randomly

Hello, This is my code: nb_lignes=`wc -l $1 | cut -d " " -f1` for i in $(seq $nb_lignes) do m=`head $1 -n $i | tail -1` //command done Please how can i change it to get Get 20% of lines in File randomly to apply "command" on each line ? 20% or 40% or 60 % (it's a parameter) Thank you. (15 Replies)
Discussion started by: chercheur857
15 Replies

8. Shell Programming and Scripting

Select lines from a file based on a criteria

Hi I need to select lines from a txt file, I have got a line starting with ZMIO:MSISDN= and after a few line I have another line starting with 'MOBILE STATION ISDN NUMBER' and another one starting with 'VLR-ADDRESS' I need to copy these three lines as three different columns in a separate... (3 Replies)
Discussion started by: Tlcm sam
3 Replies

9. Shell Programming and Scripting

How to select lines randomly without replacement in UNIX?

Dear Folks I have one column of 15000 lines and want to select randomly 5000 of them in five different times without replacement. I am aware that command 'shuf' and 'sort -R' could select randomly those lines but I am not sure how could I avoid the replacement of selection line. Does anyone have... (10 Replies)
Discussion started by: sajmar
10 Replies

10. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies
COMM(1) 						    BSD General Commands Manual 						   COMM(1)

NAME
comm -- select or reject lines common to two files SYNOPSIS
comm [-123f] file1 file2 DESCRIPTION
The comm utility reads file1 and file2, which should be sorted lexically, and produces three text columns as output: lines only in file1; lines only in file2; and lines in both files. The filename ``-'' means the standard input. The following options are available: -1 Suppress printing of column 1. -2 Suppress printing of column 2. -3 Suppress printing of column 3. -f Fold case in line comparisons. Each column will have a number of tab characters prepended to it equal to the number of lower numbered columns that are being printed. For example, if column number two is being suppressed, lines printed in column number one will not have any tabs preceding them, and lines printed in column number three will have one. comm assumes that the files are lexically sorted; all characters participate in line comparisons. EXIT STATUS
comm exits 0 on success, >0 if an error occurred. SEE ALSO
cmp(1), diff(1), sort(1), uniq(1) STANDARDS
The comm utility conforms to IEEE Std 1003.2-1992 (``POSIX.2''). BSD
June 6, 1993 BSD
All times are GMT -4. The time now is 07:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy