Quick step-by-step guide:
Get a random sample of 100 lines per text file on Mac OSX:
Steps 1 to 4 only have to be followed once per computer. After that only steps 6 & 8 are needed.
- Open Terminal window:
- Install „Homebrew“ package manager (this allows you to install additional Unix/Linux programmes on your Mac). Copy and paste the following line into the Terminal window (all one line):
ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
Source: http://brew.sh/ (for further documentation)
If it asks you to install “Commandline developer tools”, say YES (might take a while).
- Wait for installation to finish, press RETURN and enter your password (the one you use to log on to your Mac).
- Type: brew install coreutils
- Extract attached zip to your Desktop (make sure that the folder random_sample is visible on your Desktop and that there is a file called test.txt in it.
- Go back to Terminal window and type: cd Desktop/random_sample
- And now comes the actual shuffling bit: gshuf -n 2 test.txt
Instead of test.txt you can use your query results and instead of 2, you can enter the size of your sample.
- If you want to save the sample into a new text file instead of just displaying it in the terminal window, type: gshuf -n 2 test.txt > random_sample1.txt and the results will be saved in the file txt in the same folder (feel free to adapt filenames and be aware of the fact that if you use one name twice the contents of the file with the same name will be overwritten).
Explanation of the different parts of the command:
shuffle command | sample size (display shuffled lines, up to the line number specified by -n switch) | name of file you want to shuffle (lines) | write output into file | name of output file |
gshuf | -n SAMPLESIZE | test.txt | > | out.txt |
An easy way to navigate to a particular folder: type cd [space] into the terminal window, drag&drop the folder you want to work in from your Finder into the Terminal and press RETURN/ENTER.
Other basic folder/directory navigation from Terminal window:
Source: http://www.cheatography.com/davechild/cheat-sheets/linux-command-line/
Example:
test.txt | gshuf -n 2 test.txt |
line 1: Aarau
line 2: Basel line 3: Bern line 4: Luzern line 5: Olten line 6: St. Gallen line 7: Zürich |
Command for a sample of 100:
cd path_to_folder_with_file_you_want_to_shuffle
gshuf -n 100 results.txt > random_sample1.txt