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
Assemble the Command. The sort command in the middle sorts the prepared line by the first field, which contains the random numbers we prepended. Let's use the powerful awk command to prepend a random number on each line: $ awk 'BEGIN{srand()}{print rand(), $0}' input.
The shuf command in Linux writes a random permutation of the input lines to standard output. It pseudo randomizes an input in the same way as the cards are shuffled.
From man bash : -s If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell.
You should use shuf
command =)
cat file1 file2 | shuf
Or with Perl :
cat file1 file2 | perl -MList::Util=shuffle -wne 'print shuffle <>;'
Sort: (similar lines will be put together)
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-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With