LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-30-2015, 09:53 AM   #1
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 132

Rep: Reputation: Disabled
Quick GREP question..


Hey guys,

Something is puzzling me!

I saw someone use the grep in the following way and I'm not sure I understand what it does, and if there's any benefit to using it this way.

Code:
grep X.X.X.X /var/log/log.log | grep -v query
I checked the man file which confirmed that -v is relating to matching non grouping lines (which I'm not sure I fully understand either!) but I don't see any difference in the output of the above command versus the same command without the | grep -v query bit..

Why would you pipe grep into grep unless you were searching for something specific within the search results?

Does query mean something else?
 
Old 04-30-2015, 09:57 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
grep -v says to exclude the item (i.e. does the opposite of what grep normally does). In this case the user was trying to get output that excluded the word "query".

The most common time you'll see "grep -v" is "grep -v grep" when looking at processes with the ps command.
e.g. ps -ef |grep myprocess |grep -v grep
The reason for this is the line "grep myprocess" is itself a process that the grep would find more often than not and you don't want that - you just want the lines that have myprocess.
 
1 members found this post helpful.
Old 04-30-2015, 09:57 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,838

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
Give me X.X.X.X that don't contain "query".
The implication is that "query" is in other lines as well.
 
1 members found this post helpful.
Old 04-30-2015, 09:58 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
a practical example:
Code:
[schneidz@hyper ~]$ cat jonnybinthemix.txt 
hello
world
johnnyb
inthemix
this is the line i want
l33t
h4x0rz
this is the line i dont want
chun-li
akuma
[schneidz@hyper ~]$ grep "this is the line" jonnybinthemix.txt 
this is the line i want
this is the line i dont want
[schneidz@hyper ~]$ grep "this is the line" jonnybinthemix.txt | grep -v "dont want"
this is the line i want
 
Old 04-30-2015, 09:59 AM   #5
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 132

Original Poster
Rep: Reputation: Disabled
Awesome, that explains it perfectly!!

Thanks for your help, I've got it, and yes... very handy
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick grep question jonnybinthemix Linux - Newbie 2 07-29-2014 10:04 AM
[Quick question]Problem with execl and GREP pfpietro Programming 6 09-03-2012 02:10 PM
quick grep question provkitir Linux - Software 4 11-25-2004 10:32 PM
samba smb.config question (quick question) TheDOGG Linux - Networking 1 03-02-2004 08:19 AM
Quick grep question player_2 Linux - Software 2 10-10-2003 07:38 AM


All times are GMT -5. The time now is 06:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration