LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to break down a grep command (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-break-down-a-grep-command-873869/)

dimeetrees 04-08-2011 06:42 PM

How to break down a grep command
 
What does this command mean?

grep 'GET / HTTP.*Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1' last10m |cut -d ' ' -f1 |sort -n |uniq -c|sort -n|tail

Whats the best way to figure out these commands in general? I have a lot of learning to do!

Tinkster 04-08-2011 06:44 PM

Hi, welcome to LQ!

I'd suggest you run it, and try to determine what it does by
the results; you may find the man-pages of each of the tools
a great help in understanding what they do.


Cheers,
Tink

markush 04-08-2011 06:50 PM

Hello dimeetrees, welcome to LQ,

the part
Code:

'GET / HTTP.*Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1'
seems to be the string which is searched for. The rest of the command includes other Linux-tools, I'd recommend to read the manpages
Code:

man bash
man sort
man cut
man tail
...

Markus

dimeetrees 04-08-2011 06:57 PM

I'm a complete newbie in the linux terminal, and my hosting provider keeps giving me some commands to use and the output doesn't make it easier, for example:

194 58.91.131.10
198 95.220.68.95
206 200.60.251.151
209 113.22.68.87
225 87.21.218.25
248 109.185.177.60
256 84.94.63.202
275 65.100.210.134
297 95.170.191.6
314 190.178.139.12


There's nothing explaining what the numbers to the left of the ip's mean. When I try to get help in linux it doesn't really make it easier either. Is there some sort of definitive manual you would recommend that makes it easier to figure this stuff out?

Quote:

Originally Posted by Tinkster (Post 4318585)
Hi, welcome to LQ!

I'd suggest you run it, and try to determine what it does by
the results; you may find the man-pages of each of the tools
a great help in understanding what they do.


Cheers,
Tink


7sicks 04-08-2011 10:47 PM

suggestion
 
"grep 'GET / HTTP.*Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1' last10m |cut -d ' ' -f1 |sort -n |uniq -c|sort -n|tail"

grep searching the file last10m (a website request log from the looks of it) for the string 'GET / HTTP.*Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1'. Every | starts a new command, and it looks like they are filtering and sorting the results. Tail just shows the last 10 lines of the output, to prevent screen scrolling, or to show the most recent data. Couldn't tell you what exactly the first column means. Try tail last10m and maybe you can figure it out looking at the full entries rather than the filtered output.

Brian

Tinkster 04-08-2011 11:07 PM

Quote:

Originally Posted by 7sicks (Post 4318731)
"grep 'GET / HTTP.*Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1' last10m |cut -d ' ' -f1 |sort -n |uniq -c|sort -n|tail"

grep searching the file last10m (a website request log from the looks of it) for the string 'GET / HTTP.*Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1'. Every | starts a new command, and it looks like they are filtering and sorting the results. Tail just shows the last 10 lines of the output, to prevent screen scrolling, or to show the most recent data. Couldn't tell you what exactly the first column means. Try tail last10m and maybe you can figure it out looking at the full entries rather than the filtered output.

Brian

Code:

sort -n |uniq -c|sort -n
This gives it away; sort numeric; count the number of unique occurrences
of each IP, and sort that numerically in ascending order. In other words:

The whole thing tells you the top 10 IPs visiting your site, and just how
often they did visit, w/ the most frequent one at the bottom.


Cheers,
Tink


P.S.: Please, OP, pretty please; do not "top post" - it's a nasty habit
in e-Mail, and it's even uglier here because it adds no value whatsoever.

dimeetrees 04-09-2011 12:10 AM

Thanks for the responses and explanations. Sorry about the top post tinker, I could see what you mean.

dimeetrees 04-09-2011 12:11 AM

Would you guys recommend any resources on learning linux inside and out? Something that uses examples and explains all the concepts?

markush 04-09-2011 04:05 AM

Well, install Slackware http://www.slackware.org/ and take a look into the Slackware-part of LQ: http://www.linuxquestions.org/questions/slackware-14/
Also read the Slackbook: http://www.slackbook.org/
This will teach you Linux.

Markus

Tinkster 04-09-2011 04:17 PM

Quote:

Originally Posted by dimeetrees (Post 4318772)
Would you guys recommend any resources on learning linux inside and out? Something that uses examples and explains all the concepts?

Just going back to the initial question: one way to
finding out what is going on really is to use the man-
page, and then disect the command line.


Code:

grep 'GET / HTTP.*Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1' last10m |cut -d ' ' -f1 |sort -n |uniq -c|sort -n|tail
Personally I always learn best by example, followed by
(or accompanied with) an explanation. So, in the case
above, just use the grep by itself initially, and compare
that to the actual files content. Read "man grep".

Then see what
Code:

grep 'GET / HTTP.*Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1' last10m |cut -d ' ' -f1
does, and read "man cut" to understand the options it was
invoked w/ and get a feel for what cut does.

Next, try the third one tacked on:
Code:

grep 'GET / HTTP.*Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1' last10m |cut -d ' ' -f1 |sort -n
read "man sort" alongside ... and so forth. And as it
bears an actual relation to what you're doing (need to
do) it should stick fairly well.



Cheers,
Tink


All times are GMT -5. The time now is 05:41 AM.