![]() ![]() ![]() Then, we will sort output of above while loop using sort command sort -t: -k1 -n Where $RANDOM is the environment variable, each time you query this variable you will get random number. Where, while loop will read the piped input into variable x and will iterate over all lines to generate output : as you can see $RANDOM:$x. 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 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.
0 Comments
Leave a Reply. |