Here is another solution using the grep, shuf and paste commands:
shuffle1-1.sh
#!/usr/bin/env bash input=$1 if [ $# -eq 0 ] then echo "must provide a file as 1st parameter..." exit -1 fi # split data between pos and neg values and shuffle them # in temporary files grep -v "\-1" $input | shuf > tmp_subset1 grep "\-1" $input | shuf > tmp_subsetm1 # alternate 1 and -1 line paste -d"\n" tmp_subset1 tmp_subsetm1 # cleanup rm tmp_subset1 rm tmp_subsetm1
output
# ./shuffle1-1.sh test.data 1,x,y -1,t,r 1,r,t -1,e,t # ./shuffle1-1.sh test.data 1,x,y -1,e,t 1,r,t -1,t,r # cat test.data 1,x,y -1,t,r -1,e,t 1,r,t
If your file does not have the same number of lines with 1 and -1, adding | grep 1 at the end should get rid of the blank lines:
# ./shuffle1-1.sh test.data2 1,z,z -1,e,t 1,x,y -1,t,r 1,r,t 1,Z,Z # ./shuffle1-1.sh test.data2 | grep 1 1,r,t -1,t,r 1,x,y -1,e,t 1,z,z 1,Z,Z
-1vs(+)1records? Good luck.