«Redirecting Input and Output» 8/13: What's the difference between …

$ sort lakes.txt 

and

$ cat lakes.txt | sort

?

1 Like

in the first you sort the content of the lakes.txt, in the second code sample you pipe the standard output of the file to sort

this enables us to for example do a grep first:

cat lakes.txt | grep lake | sort

so sort anything with lake in the name.

2 Likes

thanks. I get your explanation but still it does not really make sense to me.
wouldn’t your code be the same as:

sort lakes.txt | grep lake

?

the output, maybe, yes. But now we first have to sort everything, and then pipe that output to grep and grep the entries we want. Sound less efficient. Sorting is generally an expensive operation.

also, it will happen that you might have 5 pipes, or a file with 100,000 lines

the big problem is that these “commands” are entire programs written in the C language. Due to this abstraction, it might be difficult to “see” the difference.

2 Likes

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

Oops, the topic closed automatically, did you still want to reply something?

no, thanks a lot for your help! :ok_hand:

1 Like