LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 08:53 AM   #1
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

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, 08:57 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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, 08:57 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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, 08:58 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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, 08:59 AM   #5
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

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

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


Reply



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 09:04 AM
[Quick question]Problem with execl and GREP pfpietro Programming 6 09-03-2012 01:10 PM
quick grep question provkitir Linux - Software 4 11-25-2004 09:32 PM
samba smb.config question (quick question) TheDOGG Linux - Networking 1 03-02-2004 07:19 AM
Quick grep question player_2 Linux - Software 2 10-10-2003 06:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:42 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
Open Source Consulting | Domain Registration