randomly shuffling files in bash

2019-01-08 20:16发布

I have some files in linux. For example 2 and i need shuffling the files in one file.

For example

$cat file1
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8

and

$cat file2
linea one
linea two
linea three
linea four
linea five
linea six
linea seven
linea eight

And later that i shuffling the two files i can obtain something like:

linea eight
line 4
linea five
line 1
linea three
line 8
linea seven
line 5
linea two
linea one
line 2
linea four
line 7
linea six
line 1
line 6

8条回答
Bombasti
2楼-- · 2019-01-08 20:29

It is clearly biased rand (like half the time the list will start with the first line) but for some basic randomization with just bash builtins I guess it is fine? Just print each line yes/no then print the rest...

shuffle() {
    local IFS=$'\n' tail=
    while read l; do
        if [ $((RANDOM%2)) = 1 ]; then
            echo "$l"
        else
            tail="${tail}\n${l}"

        fi
    done < $1
    printf "${tail}\n"
}
查看更多
我命由我不由天
3楼-- · 2019-01-08 20:35

Sort:

cat file1 file2 | sort -R

Shuf:

cat file1 file2 | shuf

Perl:

cat file1 file2 | perl -MList::Util=shuffle -e 'print shuffle<STDIN>'

BASH:

cat file1 file2 | while IFS= read -r line
do
    printf "%06d %s\n" $RANDOM "$line"
done | sort -n | cut -c8-

Awk:

cat file1 file2 | awk 'BEGIN{srand()}{printf "%06d %s\n", rand()*1000000, $0;}' | sort -n | cut -c8-
查看更多
beautiful°
4楼-- · 2019-01-08 20:38

Here's a one-liner that doesn't rely on shuf or sort -R, which I didn't have on my mac:

while read line; do echo $RANDOM $line; done < my_file | sort -n | cut -f2- -d' '

This iterates over all the lines in my_file and reprints them in a randomized order.

查看更多
\"骚年 ilove
5楼-- · 2019-01-08 20:38

You don't need to use pipes here. Sort alone does this with the file(s) as parameters. I would just do

sort -R file1

or if you have multiple files

sort -R file1 file2
查看更多
迷人小祖宗
6楼-- · 2019-01-08 20:40

You should use shuf command =)

cat file1 file2 | shuf

Or with Perl :

cat file1 file2 | perl -MList::Util=shuffle -wne 'print shuffle <>;'
查看更多
贼婆χ
7楼-- · 2019-01-08 20:41

Just a note to OS X users who use MacPorts: the shuf command is part of coreutils and is installed under name gshuf.

$ sudo port install coreutils
$ gshuf example.txt # or cat example.txt | gshuf
查看更多
登录 后发表回答