Mailinglist Archive: opensuse (3784 mails)
| < Previous | Next > |
RE: [SLE] OT: unsorted list of numbers
- From: (Ted Harding) <Ted.Harding@xxxxxxxxxxxxxxxx>
- Date: Sat, 13 Oct 2001 14:29:21 +0100 (BST)
- Message-id: <XFMail.011013142921.Ted.Harding@xxxxxxxxxxxxxxxx>
Hi,
On 12-Oct-01 Ted Harding wrote:
> If you want a quick one-off one-liner, the following does it nicely:
>
> Suppose your file, with a separate original number in each line,
> is called "temp". Then:
>
> cat temp |
> awk 'BEGIN{srand()} {$2=$1; $1=rand(); print $0}' |
> sort -n |
> awk '{print $2}'
>
> will do it. This basically outputs the original lines in
> a random order. (In fact, they don't need to be numbers
> thnough, as written, the above will fail if they are text
> lines with spaces since the default field separator in awk
> is the space; however, if needed, this can be worked round
> by changing awk's internal FS and OFS variables in the "awk"
> invocations).
Following the above, I've extended the method along the lines
suggested. The following two scripts will:
(a) output the input file with the lines rearranged in random order,
whether these lines are numbers or text;
(b) make a random selection of a given number out of the lines
in the file.
Copy the scripts into the named files and make them executable
(chmod 755).
(a) rand_perm_lines
#! /bin/bash
cat $1 |
awk 'BEGIN{FS=SUBSEP; OFS=SUBSEP; srand()} \
{$2=$1; $1=rand(); print $0}' |
sort -n |
awk 'BEGIN{FS=SUBSEP;OFS=" "}{print $2}'
(b) rand_select_lines
#! /bin/bash
cat $1 |
awk 'BEGIN{FS=SUBSEP; OFS=SUBSEP; srand()} \
{$2=$1; $1=rand(); print $0}' |
sort -n |
awk 'BEGIN{FS=SUBSEP;OFS=" "}{print $2}' |
head -n $2
(note that (b) is simply (a) with a bit tagged on).
Usage:
(a) rand_perm_lines filename
(b) rand_select_lines filename N
(where N is the number of lines you want to select).
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding@xxxxxxxxxxxxxxxx>
Fax-to-email: +44 (0)870 167 1972
Date: 13-Oct-01 Time: 14:29:21
------------------------------ XFMail ------------------------------
On 12-Oct-01 Ted Harding wrote:
> If you want a quick one-off one-liner, the following does it nicely:
>
> Suppose your file, with a separate original number in each line,
> is called "temp". Then:
>
> cat temp |
> awk 'BEGIN{srand()} {$2=$1; $1=rand(); print $0}' |
> sort -n |
> awk '{print $2}'
>
> will do it. This basically outputs the original lines in
> a random order. (In fact, they don't need to be numbers
> thnough, as written, the above will fail if they are text
> lines with spaces since the default field separator in awk
> is the space; however, if needed, this can be worked round
> by changing awk's internal FS and OFS variables in the "awk"
> invocations).
Following the above, I've extended the method along the lines
suggested. The following two scripts will:
(a) output the input file with the lines rearranged in random order,
whether these lines are numbers or text;
(b) make a random selection of a given number out of the lines
in the file.
Copy the scripts into the named files and make them executable
(chmod 755).
(a) rand_perm_lines
#! /bin/bash
cat $1 |
awk 'BEGIN{FS=SUBSEP; OFS=SUBSEP; srand()} \
{$2=$1; $1=rand(); print $0}' |
sort -n |
awk 'BEGIN{FS=SUBSEP;OFS=" "}{print $2}'
(b) rand_select_lines
#! /bin/bash
cat $1 |
awk 'BEGIN{FS=SUBSEP; OFS=SUBSEP; srand()} \
{$2=$1; $1=rand(); print $0}' |
sort -n |
awk 'BEGIN{FS=SUBSEP;OFS=" "}{print $2}' |
head -n $2
(note that (b) is simply (a) with a bit tagged on).
Usage:
(a) rand_perm_lines filename
(b) rand_select_lines filename N
(where N is the number of lines you want to select).
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding@xxxxxxxxxxxxxxxx>
Fax-to-email: +44 (0)870 167 1972
Date: 13-Oct-01 Time: 14:29:21
------------------------------ XFMail ------------------------------
| < Previous | Next > |