randomize one column in a sorted file

November 15, 2010 at 02:26:34
Specs: Linux x86_64
I need to shuffle a field of a file with matched columns.
1990 5
1991 4
1992 3
1993 7
1994 9
1995 11

the shuf command unsorts the entire file and keeps the relationship between fields. I need to unmatch the columns with a random permutation of just one column

See More: randomize one column in a sorted file

November 15, 2010 at 10:57:19
Sorry, but I don't have time to provide a total solution. I can give you part of it. This bash script prints out a random line of a file. Your Linux system may not have the cksum command in the same location or you may not even have cksum. All I am doing is trying to seed the RANDOM number generator. you can change that.

What you have to do is set up a loop around the number generator, keep track of the lines that have been printed, and stop the loop when all lines have been printed:

Let me know if you have any questions:


# how many lines are there
counter=$(wc -l < ${myfile})

# seed the RANDOM number generator
s1=$(/usr/bin/ps -elfy)
s2=$(/usr/bin/cat -s /etc/shadow)
RANDOM=$(echo $s1 $s2|/usr/bin/cksum|/usr/bin/cut -b1-8)

# generate a random number up to the total number of files
number=$(($RANDOM % $counter + 1))
# echo $number

# print the line number from the file
sed -n "${number}"p ${myfile}

Report •
Related Solutions

Ask Question