Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to create a sorted list of all TCP services found in the file /etc/services. Pipe the output of the command grep tcp /etc/services into the command sort. Redirect the output of this pipe into the file ~/pipelab.txt.

I don't know what I am doing wrong. I keep getting a error message that my output is wrong?

This is what I tried last: grep tcp /etc/services > ~/pipelab.txt | sort

Thank you for any help.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

> ~/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
share|improve this answer

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).

share|improve this answer
    
Thank you very much, I thought as long as I put the sort command in anywhere it would still work. –  user72510 Jun 20 at 17:03
1  
@user72510 Pipelines are processed in order, from left to right. That's actually useful, for example if you want the first five lines of the sorted output, you could do grep tcp /etc/services | sort | head -n 5. Its important the sort be done before the head, or you'd not get the lines you want. –  derobert Jun 20 at 17:09
    
Thank you for explaining that. I am taking the class online so I am teaching myself. Adding to it that I am 40 it is slow learning. –  user72510 Jun 20 at 17:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.