Tag: sort

  • Nice tips of unix command tools 123 398…

    rencontres seniors à eysines Nice tips of unix command tools

    123 398 17359 317 19 2909 39 -399 -5789 49 33 200 255 33 -378

    sort -n file 
    

    Sort the file by first column. The -n option ensures numeric (as opposed to lexicographic) sort.

    sort -k 2 -n file 
    

    Sort the file by second column. The “-k” option here denotes the column used as sort key.

    grep '33' file 
    

    Extract all lines containing the string “33” (in the above example, lines 4 and 5).

    grep -c '33' file 
    

    Same, but display only the number of matching lines (2 in the example), not the lines themselves. This is useful to analyze large data files of output data. For example, if a sequence of one million integers, is saved as a file, one per line, “grep -c ’33’ file” will display the number of 0’s in that sequence.

    grep -c '-' *.out 
    

    Same command, but applied to all files in the current directory matching “*.out”. For each file there is an output line of the form “Filename: x”, where “x” is the number of matching lines in the file.

    sort -n file | cat -n 
    

    Sort the file, then prepend line numbers to each line. This results in the following:

         1	 39  -399  -5789
         2	 49  33   200
         3	123  398  17359
         4	255 33   -378
         5	317  19  2909
    

    http://www.math.uiuc.edu/~hildebr/computer/unixtips.html