How do I sort using the grep and sort commands?

You're trying to both redirect the output of grep to a file and pipe it to sort. You can't do that, at least not like that.

Instead, you really just want to feed it to sort:

grep tcp /etc/services | sort

and then you want to redirect the sorted output (i.e., what's coming out of sort) to a file, so you put the redirect after sort:

grep tcp /etc/services | sort > ~/pipelab.txt

Both pipes and redirects work by changing where the output of the command goes. You had two of them fighting over the output from grep (and ultimately, the redirect won, and wrote the unsorted output to your file).


> ~/pipelab.txt obviously belongs to the command on the same side of the pipeline operator |. I.e. you redirect the grep output to the file instead of piping it into sort:

grep tcp /etc/services | sort > ~/pipelab.txt

Tags:

Shell

Pipe