Grep and Sort

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

  2. Print the number of people that are in the /etc/passwd file with the name of George

  3. Sort by name and print the "who" list using pipes to link the commands.

  4. Relevant commands, code, scripts, algorithms:

grep, sort

  1. The attempts at a solution (include all code and scripts):

grep -cw "George" /etc/passwd
  1. Any advice for this? I am not sure what to do, except use something like
sort -n who

, but that does not work.

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Brookdale Community College - Lincroft, New Jersey - United States - Dr. Rick Bournique- COMP 145

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

A) For #2, the problem statement wants you to use a "pipe".
Can you explain what a pipe is, and give some examples?

B) Also, #2 mentions "who". What is "who"? Is it a file? Or what?

If you can understand A) and B), I think you can finish the assignment.

Sir, those are the assignment directions. I will have to email my teacher I guess. The directions are shit.

The assignment directions are not "shit". :slight_smile: The directions are actually very good.

But you cannot finish the assignment unless you understand what a "pipe" is, and what "who" is. You can easily figure that out on your own, with a little research and experimentation.

Did your course mention "pipe" before? If not, you could look for pipe on wikipedia, as used in the computer sense. And think about what "who" might be? Is there a file on your computer called "who"? If not, then what's the other possibility about "who"? Again, you could even find the answer on wikipedia.

Keep at it. I would not email your teacher yet.

Oh, sorry, it sounded like you were asking me what those things were because you didn't know (which you obviously do, so my apologies). I know that "Who" is a command that lists the users currently on the server and piping uses | to create a sequence of commands.

So for #2, maybe something like:

sort -n | who

It's understandable. No problem.

You understand the terms perfectly. The proposed solution is in the right direction (you're getting warm), but kind of looks like a shot in the dark. Think about it. What exactly do you want to sort?

Do you have access to a computer to test the solution? If so, what happens when you try the proposed solution? Or do you have to do this just on paper?

Oh,

-n

is for numeric values. So would it just be

sort | who

And yes I can use Putty to connect to the school server, but only myself and the server admin are usually on, so not sure if it is really working to be honest (his name just might pop up first by default).

EDIT: I tried this and it did not work, because I did a reverse order sort using

who | sort -r

and my name came first, so that works. So my answer would thus be

who | sort

. I am not really sure why this order of who and sort works. Is it because you need to run who first so the sort takes the input from the other command (who)?

P.S. I am doing a small grading program for this same lab as well. I will post my results later tonight. Didn't post here because I had not yet tried anything. Should I open a new thread or post here?

The output of the first command gets funneled into the input of the second command; that's just how pipes work in shell.

If you do sort | who that's trying to put the output from sort into the input of who, which is a command that doesn't even read anything, so it doesn't make much sense.

You got it. :b:

$ who | sort

You probably already know the best way to learn is to log on and play around on the computer. You're right if only two people are on (you and the admin) that you can't really test "who" very well. In that case, you can create a little test file, and do some experiments, such as:

$ echo Joe > test.txt
$ echo Bill >> test.txt
$ echo Suzy >> test.txt
$ echo Alice >> test.txt
$ cat test.txt
Joe
Bill
Suzy
Alice
$ sort test.txt
Alice
Bill
Joe
Suzy
$ cat test.txt | sort
Alice
Bill
Joe
Suzy
$ sort < test.txt
Alice
Bill
Joe
Suzy

I used "echo" to make the test.txt file. If you know how to use a Unix editor, of course use the editor instead.

With the test file, you can try out different sort options, as you are already doing with -r option.

Yes, that is exactly right! "who" produces the output, the "raw materials". The output from "who" enters the pipe. On the other side of the pipe, "sort" reads its input. The output from "sort" is what you finally see.

"who" does not expect any input, so it would not make sense to put "who" on the right-hand side of the pipe. "sort" HAS to have input, or have a file specified for it to operate upon. In the examples above, I showed three simple ways sort can get input (sort file, | sort, and sort < file).

You should open a new thread.