简体   繁体   中英

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.

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 (eg, 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

you can use this command:

cut -f 2 practise.sam | uniq | sort > sorted.txt

In your code is wrong. The fault is "No such file or directory". Because of pipe. You can learn at this link how it is used

https://www.guru99.com/linux-pipe-grep.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM