LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Linux command (https://www.linuxquestions.org/questions/linux-newbie-8/linux-command-4175580287/)

torito 05-20-2016 02:01 PM

Linux command
 
Hi guys,
For my homework I have to explain the following command:

cut –f 5 –d: /etc/passwd | grep ‘^K’ | wc –l

Would you you please help me out?
Thank you!

rtmistler 05-20-2016 02:06 PM

LQ is not here to answer homework questions for you.

How about you offer "your" explanation? People may then offer any corrections or statements about varied interpretations.

And have you run this command sequence? Have you tried any part of it? Have you looked up what cut, pipe, and grep all do?

torito 05-20-2016 02:24 PM

Yes I did, in fact I am an old man trying to learn, I am not trying just go by.

My understanding is the command will first search for delimeter ":" and then split the contents into fields, in this case are 5 fields. Fields 1 to 4 contents will be then deleted so it will display the contents from fifth field by end of line of each line. Then this output will be sent as input to grep. After that I am stuck! I don;t know how to finish it.
Thank you for your help!

rtmistler 05-20-2016 02:40 PM

Well look at just the grep command, it is searching for ^K within the string extracted by the cut command. The wc portion gives you a count of newlines, thus how many valid search results were detected because they will have been delimited by newlines.

A further suggestion is in your terminal to first type "set -xv" to enable verbose and debug in the shell. This is a per session thing, so once you exit that terminal, new terminals will not have those settings, unless your environment is set up that way, but that is non-typical.

What does it do? An example of a simple grep:
Code:

set -xv
grep 123 1.txt | wc -l
+ grep 123 1.txt
+ wc -l
1

This shows the FLOW of that command string, and will do the same for your command sequence.

dab1414 05-20-2016 02:41 PM

LINK here is some info on grep, and it has an example using the carat "^"

suicidaleggroll 05-20-2016 02:45 PM

I suggest you run the following commands, start with the basics and then build up to see what each addition changes. Only move to the next command once you understand what's going on with the current one.
Code:

cat /etc/passwd
cut -f 5 -d: /etc/passwd
cut -f 5 -d: /etc/passwd | grep '^K'
cut -f 5 -d: /etc/passwd | grep '^K' | wc -l


torito 05-20-2016 02:46 PM

Thank you so much rtmistler for your detailed explanation! Also thank you dab1414 for the link. suicidaleggroll, thank you for your suggestion. As a new in to this Linux world, that is the best way to do it. The problem is that I want to learn to fast....

Thank you again!

rtmistler 05-20-2016 03:03 PM

Quote:

Originally Posted by torito (Post 5548405)
Thank you so much rtmistler for your detailed explanation! Also thank you dab1414 for the link. suicidaleggroll, thank you for your suggestion. As a new in to this Linux world, that is the best way to do it. The problem is that I want to learn to fast....

Thank you again!

We get that, but as you can see, you're going to get better results when you show you efforts. Saying the keyword "homework" may be a mistake given how you described in your follow up. Instead say, "Hey, I'm trying to self learn, and this is what I'm looking at ..." and then do try to include some details on your efforts. Believe me, you'll get far more respectful and helpful answers. Also be aware that some answers may not show up so very fast. Long, long ago I asked a doozy. I asked it fairly detailed, just no one had any answers. I sort of self-solved it in another manner and literally months later someone offered thoughts. It was at that point where I learned to follow-up and update my original question as Solved and also to include my solution on the matter. It helps.

Best of luck with the studies and your efforts. :)

torito 05-20-2016 03:15 PM

I now completely understand rtmistler. I am not a very experienced user when using forums plus as I said before English is my second language, so in many cases my words are npot the meaning I am looking for. Thank you again for your help! I am looking forward of participating in this great forum!

JJJCR 05-21-2016 02:21 AM

Quote:

Originally Posted by torito (Post 5548382)
Hi guys,
For my homework I have to explain the following command:

cut –f 5 –d: /etc/passwd | grep ‘^K’ | wc –l

Would you you please help me out?
Thank you!

man cut

man grep

man wc

check out:
https://www.youtube.com/watch?v=nLa6jAbULe8
http://how-to.linuxcareer.com/learni...x-commands-cut


All times are GMT -5. The time now is 08:09 AM.