I have the following line in a file : [[0, [0, '1']], [1, [0, '2']], [2, [0, '3']], [3, [0, '4']]][[0, [1, '5']], [1, [1, '6']], [2, [1, '7']], [3, [1, '8']]][[0, [2, '9']], [1, [2, '10']], [2, [2, '11']], [3, [2, '12']]]Is there a way to sort them on the first character (0,1,2 or 3) in the inner list element so that I will end up with the following: [[0, [0, '1']], [0, [1, '5']],[0, [2, '9']]][[1, [0, '2']], [1, [1, '6']],[1, [2, '10']]] and so on? 
How to perform custom sort
sort
Well, this is not particularly elegant but it will do what you need. At least, it does on the example you posted:perl -ne 'while(s/(\[(\d),\s*\[.+?\]\])//){push @{$k{$2}},$1 . ,;}           END{ print [@{$k{$_}}]\n for keys(%k)}' file.txt | sed 's/,\]$/\]/' | sortRunning the command above on a file called file.txt that contains the lines from your example gives the following output:[[0, [0, '1']], [0, [1, '5']], [0, [2, '9']]][[1, [0, '2']], [1, [1, '6']], [1, [2, '10']]][[2, [0, '3']], [2, [1, '7']], [2, [2, '11']]][[3, [0, '4']], [3, [1, '8']], [3, [2, '12']]]ExplanationI will not explain in detail since this is actually a small program and uses quite a few features of Perl. The basic idea of the Perl script is to find all cases of [\d, [.+?]] where \d is any number and .+?]] is everything up to the first ]]. This will basically collect all the different sub-arrays or tuples or whatever these are and save them in a hash of arrays whose key is the first digit, the one you want to sort on. Once the whole file has been processed, it will print each array of the hash. This is the output of the script alone:$ perl -ne 'while(s/(\[(\d),\s*\[.+?\]\])//){push @{$k{$2}},$1 . ,;}           END{ print [@{$k{$_}}]\n for keys(%k)}' file.txt [[1, [0, '2']], [1, [1, '6']], [1, [2, '10']],][[3, [0, '4']], [3, [1, '8']], [3, [2, '12']],][[0, [0, '1']], [0, [1, '5']], [0, [2, '9']],][[2, [0, '3']], [2, [1, '7']], [2, [2, '11']],]I am separating each sub array with a , but that means I get an extra one at the end so I use sed to remove that. $ perl -ne 'while(s/(\[(\d),\s*\[.+?\]\])//){push @{$k{$2}},$1 . ,;} >           END{ print [@{$k{$_}}]\n for keys(%k)}' file.txt | > sed 's/,\]$/\]/' [[1, [0, '2']], [1, [1, '6']], [1, [2, '10']]][[3, [0, '4']], [3, [1, '8']], [3, [2, '12']]][[0, [0, '1']], [0, [1, '5']], [0, [2, '9']]][[2, [0, '3']], [2, [1, '7']], [2, [2, '11']]]Finally, you also wanted this sorted but since they now are organized by their first character, a simple sort will do:$ perl -ne 'while(s/(\[(\d),\s*\[.+?\]\])//){push @{$k{$2}},$1 . ,;}           END{ print [@{$k{$_}}]\n for keys(%k)}' file.txt | sed 's/,\]$/\]/' | sort[[0, [0, '1']], [0, [1, '5']], [0, [2, '9']]][[1, [0, '2']], [1, [1, '6']], [1, [2, '10']]][[2, [0, '3']], [2, [1, '7']], [2, [2, '11']]][[3, [0, '4']], [3, [1, '8']], [3, [2, '12']]]