Search code examples
linuxsortingpipelinecutuniq

Pipelining cut sort uniq


Trying to get a certain field from a sam file, sort it and then find the number of unique numbers in the file. I have been trying:

cut -f 2 practice.sam > field2.txt | sort -o field2.txt sortedfield2.txt |  
uniq -c sortedfield2.txt

The cut is working to pull out the numbers from field two, however when trying to sort the numbers into a new file or the same file I am just getting a blank. I have tried breaking the pipeline into sections but still getting the same error. I am meant to use those three functions to achieve the output count.


Solution

  • Use

    cut -f 2 practice.sam | sort -o | uniq -c
    

    In your original code, you're redirecting the output of cut to field2.txt and at the same time, trying to pipe the output into sort. That won't work (unless you use tee). Either separate the commands as individual commands (e.g., use ;) or don't redirect the output to a file.

    Ditto the second half, where you write the output to sortedfield2.txt and thus end up with nothing going to stdout, and nothing being piped into uniq.

    So an alternative could be:

    cut -f 2 practice.sam > field2.txt ; sort -o field2.txt sortedfield2.txt ; uniq -c sortedfield2.txt
    

    which is the same as

    cut -f 2 practice.sam > field2.txt 
    sort -o field2.txt sortedfield2.txt 
    uniq -c sortedfield2.txt