Sponsored Content
Top Forums UNIX for Dummies Questions & Answers How to randomly select lines from a text file Post 302720475 by drl on Wednesday 24th of October 2012 09:00:25 AM
Old 10-24-2012
Hi.

There are a number of commonly-available utilities to do this. Here is a demonstration of two:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate random selection of lines with rl, shuf.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C rl shuf

pl " Lines selected from:"
cat data0

# Prepare data file from single line of words.
tr ' ' '\n' < data0 > data1

pl " Results from shuf, 1:"
shuf -n 3 data1

pl " Results from shuf, 2:"
shuf -n 3 data1

pl " Results from rl, 1:"
rl -c 3 data1

pl " Results from rl, 2:"
rl -c 3 data1

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
rl 0.2.7
shuf (GNU coreutils) 6.10

-----
 Lines selected from:
foo bar baz qux quux corge grault garble warg fred plugh xyzzy thud

-----
 Results from shuf, 1:
quux
thud
qux

-----
 Results from shuf, 2:
thud
xyzzy
quux

-----
 Results from rl, 1:
corge
qux
grault

-----
 Results from rl, 2:
thud
corge
warg

You may need to install these from your distribution repository. See man pages for details.

See also Algorithm::Numerical::Sample - search.cpan.org if a perl module is desirable.

Best wishes ... cheers, drl
This User Gave Thanks to drl 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
TRAMPOLINE(3)						     Library Functions Manual						     TRAMPOLINE(3)

NAME
trampoline - closures as first-class C functions SYNOPSIS
#include <trampoline_r.h> function = alloc_trampoline_r(address, data0, data1); free_trampoline_r(function); is_trampoline_r(function) trampoline_r_address(function) trampoline_r_data0(function) trampoline_r_data1(function) DESCRIPTION
These functions implement closures as first-class C functions. A closure consists of a regular C function and a piece of data which gets passed to the C function when the closure is called. Closures as first-class C functions means that they fit into a function pointer and can be called exactly like any other C function. func- tion = alloc_trampoline_r(address, data0, data1) allocates a closure. When function gets called, it stores in a special "lexical chain reg- ister" a pointer to a storage area containing data0 in its first word and data1 in its second word and calls the C function at address. The function at address is responsible for fetching data0 and data1 off the pointer. Note that the "lexical chain register" is a call-used register, i.e. is clobbered by function calls. This is much like gcc's local functions, except that the GNU C local functions have dynamic extent (i.e. are deallocated when the creating function returns), while trampoline provides functions with indefinite extent: function is only deallocated when free_trampoline_r(func- tion) is called. is_trampoline_r(function) checks whether the C function function was produced by a call to alloc_trampoline_r. If this returns true, the arguments given to alloc_trampoline_r can be retrieved: trampoline_r_address(function) returns address, trampoline_r_data0(function) returns data0, trampoline_r_data1(function) returns data1. SEE ALSO
trampoline(3), gcc(1), varargs(3) PORTING
The way gcc builds local functions is described in the gcc source, file gcc-2.6.3/config/cpu/cpu.h. AUTHOR
Bruno Haible <bruno@clisp.org> ACKNOWLEDGEMENTS
Many ideas were cribbed from the gcc source. 22 October 1997 TRAMPOLINE(3)
All times are GMT -4. The time now is 09:17 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy