Changes between Version 3 and Version 4 of Linux/Shell/ReverseOrderOfList


Ignore:
Timestamp:
02/08/08 23:02:17 (9 years ago)
Author:
tj
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Linux/Shell/ReverseOrderOfList

    v3 v4  
    11= Reverse Order Of List = 
     2 
     3'''Note:''' since writing this memo I re-discovered '''tac''' (reverse cat) in `coreutils`! 
    24 
    35Sometimes it is useful to quickly reverse the order of a list (often lines of text) in a bash shell or script. Unfortunately the tools that seem most appropriate, `sort` and `rev`, aren't. `sort -r` reverses the comparison logic whilst sorting, rather than just swapping last to first, etc., and `rev` will only reverse the characters on a line. 
    46 
    5 Using a bash array and the `seq` utility is the solution. 
     7 
     8== Bash Array == 
     9Using a bash array and the `seq` utility is one solution. 
    610 
    711 1. Read the lines into an array 
     
    1822}}} 
    1923Note the use of `seq [first] [increment] [last]`. In this case `first` is the number of elements in the array (${#patches[@]} - 1), `increment` is -1, and `last` is 0. 
     24 
     25== tac == 
     26The ''proper'' way though, is of course: 
     27{{{ 
     28$ for patch in $(grep -v '^#' debian/patches/series | tac); \ 
     29  do echo "Reversing ${patches[$patch]}"; patch -R -p1 <debian/patches/${patches[$patch]}; done 
     30}}}