First command cat will read the file content and will pipe it to shell while loop while read x do echo $RANDOM:$x done Now, lets come to see how this command work. Let’s take a look at following command, $ cat shuffle_mylines.txt | while read x do echo $RANDOM:$x done | sort -t: -k1 -n | sed 's/^*://' How does it work?Ĭommands we have used in the above example are, We can’t shuffle line using single sed command, but we will do by combining other commands. It is one of the command widely used for text processing in unix/linux. You may have already know about command sed(Stream Editor). $ shuf shuffle_mylines.txt Shuffle lines using sed You just need to call this command with file name as an argument. This command is light wight and straight forward. $ seq 10 > shuffle_mylines.txt Command shuf It checks if the current line was already seen in the text before and if so, the application deletes this duplicate.We can shuffle lines in the file in linux using following commandsĪs an example we will take a file shuffle_mylines.txt having numbers till 10 each digit in a new line. Another option that can be useful is "Skip Duplicate Lines". If any of the lines are empty, the program can quickly delete it if the option "Skip Empty Lines" is enabled. If the "Trim Lines" option is active, the utility removes trailing and leading whitespace in each line before shuffling them. This way, you can shuffle any number of lines and preserve their relative positions. For example, if you enter the number 2 in the group size option, it will glue the 1st and 2nd lines together and move them together to a new position, then it will take the 3rd and 4th lines together, then the 5th and 6th, and so on. It can also take several lines at once and move them together without splitting them apart. By default, it takes every single line in turn (1, 2, 3, …, n), generates a random number from 1 to n for it, which is the new position, and moves the line to this new place. The behavior of the algorithm changes based on how many lines are shuffled at the same time. It uses the Knuth method to do it, aka the Fisher and Yates order permutation algorithm. This tool rearranges the order of lines in the given textual data.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |