Helpful_Triazole
Helpful_Triazole

Reputation: 159

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.

Upvotes: 2

Views: 4442

Answers (2)

bozkurt.ykp
bozkurt.ykp

Reputation: 56

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

Upvotes: 0

user707650
user707650

Reputation:

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

Upvotes: 2

Related Questions