Why there is no use of cat when piping with sort?


#1

It was explained that when we pipe and cat with wc it will look like that:
cat XXXX.txt | wc | cat > YYYY.txt
and that because that the second cat receive the cat XXX | wc as input (and redirect to YYYY).

So why we don’t do the same with sort?
in sort it shows: cat XXXX.txt | sort > YYYY.txt
why not putting another | cat?

https://www.codecademy.com/courses/learn-the-command-line/lessons/redirection/exercises/sort?action=lesson_resume

Thanks for the helpers (:


#2

Those commands should be written as:

wc XXXX.txt > YYYY.txt
sort XXXX.txt > YYYY.txt

Or if those programs weren’t able to read from files directly:

wc < XXXX.txt > YYYY.txt
sort < XXXX.txt > YYYY.txt

You can chain cat indefinitely since all it does is repeat the same data, it’s often redundant.

When I use cat it’s to display a small file (cause cat will write the file to stdout which is going to show up in the terminal) - or to do the opposite, use cat to read from stdin (the terminal) and redirect to some file (allowing me to paste into a file)

Keep in mind that its namesake is concatenate, this repeating behaviour is just what you get when you concatenate a single file and is seldom needed for using other programs. Redundant use of cat is a pretty common mistake.


#3

Thank you very much Ionatan! :grin:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.