LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2015, 06:54 AM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
grep with context


A sample input file:
Code:
Audi
Bentley
Chevrolet
Dodge
QQQ
Ford
Honda
Isuzu
Jaguar
QQQ
Kaiser
Lexus
This InFile contains many lines, and a few contain QQQ.

Case 1: To extract only those lines with QQQ
Code:
grep "QQQ" $InFile
Case 2: The negative of Case 1. To extract all lines which do not contain QQQ
Code:
grep -v "QQQ" $InFile
Case 3: To extract the lines with QQQ and one preceding line
Code:
grep -B1 "QQQ" $InFile
Cases 1, 2, and 3 work as expected. Now, an attempt to extract the negative of Case 3
Code:
grep -v -B1 "QQQ" $InFile
This does not work as expected. It returns the entire input file.

Please explain.

Daniel B. Martin
 
Old 04-23-2015, 08:29 AM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Have you thought about it? You are telling grep to find every line but one and telling it to return those lines and the lines before them as well.
 
Old 04-23-2015, 08:52 AM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by SoftSprocket View Post
Have you thought about it? You are telling grep to find every line but one and telling it to return those lines and the lines before them as well.
When you think about it, it should return every line but the last one and the ones containing QQQ+the line before twice. Problem is, it doesn't, and I couldn't find any explanation for that either.

Last edited by TobiSGD; 04-23-2015 at 08:53 AM.
 
Old 04-23-2015, 09:01 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
from info grep:
Code:
2.1.5 Context Line Control
--------------------------

Regardless of how these options are set, `grep' will never print any
given line more than once.  If the `-o' (`--only-matching') option is
specified, these options have no effect and a warning is given upon
their use.
 
1 members found this post helpful.
Old 04-23-2015, 09:01 AM   #5
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
is there a bugzilla that can be logged somewhere. it probably is functions as designed but it would be nice to have the developers explanation.
 
Old 04-23-2015, 09:50 AM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by millgates View Post
from info grep:
Code:
2.1.5 Context Line Control
--------------------------

Regardless of how these options are set, `grep' will never print any
given line more than once.  If the `-o' (`--only-matching') option is
specified, these options have no effect and a warning is given upon
their use.
Thanks. So the problem is not a software problem, but a documentation problem, this information is simply not present on the manpage (and to be quite frank, I never use info, I find it uncomfortable and hard to use).
 
Old 04-23-2015, 10:03 AM   #7
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
I don't like the info pages myself and I only use them as a last resort when I can't find what I'm looking for in the man pages, just for case it happens to contain that one extra sentence that explains the problem.
 
Old 04-23-2015, 10:13 AM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by millgates View Post
I don't like the info pages myself and I only use them as a last resort when I can't find what I'm looking for in the man pages, just for case it happens to contain that one extra sentence that explains the problem.
I will keep that in mind. It never came to my mind that the manpages might not contain the same information as the info pages do.
 
Old 04-23-2015, 10:39 AM   #9
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
They usually do, but not always. Often, the man page ends with a reference to info. For example, all the way down my grep manpage says:
Code:
   TeXinfo Documentation
       The  full  documentation  for  grep  is maintained as a TeXinfo manual,
       which you can read at http://www.gnu.org/software/grep/manual/.  If the
       info and grep programs are properly installed at your site, the command

              info grep

       should give you access to the complete manual.

NOTES
       This  man  page  is maintained only fitfully; the full documentation is
       often more up-to-date.
To get back to the original question, I would suggest an alternative to the non working grep:

Code:
awk -F'[^\n]*\nQQQ\n' -vORS= -vOFS= -vRS='\f' '($1=$1)||1' <inFile

Last edited by millgates; 04-23-2015 at 10:43 AM.
 
1 members found this post helpful.
Old 04-25-2015, 07:49 AM   #10
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

I often use cgrep, and it appears to work as one might expect:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate "inverse" pattern match, cgrep.
# cgrep, see:
# http://sourceforge.net/projects/cgrep/

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C cgrep

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results, cgrep:"
cgrep -D -1 QQQ $FILE

pl " Results, inverse cgrep:"
cgrep -D -V -1 QQQ $FILE

pl " Results, inverse cgrep, wrong -v (lowercase), expect failure:"
cgrep -D -v -1 QQQ $FILE

exit 0
producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
cgrep ATT cgrep 8.15

-----
 Input data file data1:
Audi
Bentley
Chevrolet
Dodge
QQQ
Ford
Honda
Isuzu
Jaguar
QQQ
Kaiser
Lexus

-----
 Results, cgrep:
Dodge
QQQ
Jaguar
QQQ

-----
 Results, inverse cgrep:
Audi
Bentley
Chevrolet
Ford
Honda
Isuzu
Kaiser
Lexus

-----
 Results, inverse cgrep, wrong -v (lowercase), expect failure:
Audi
Bentley
Chevrolet
Dodge
QQQ
Ford
Honda
Isuzu
Jaguar
QQQ
Kaiser
Lexus
One needs to compile it. I have done that in both 32-bit and 64-bit without problems.

Best wishes ... cheers, makyo
 
1 members found this post helpful.
  


Reply

Tags
grep


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
Find & grep - how to return pathes, not grep phrases ? postcd Linux - General 2 11-25-2014 12:43 PM
grep to file outputs more than grep to screen? tcpman Linux - Server 4 06-07-2013 04:46 AM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:06 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