LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 01-02-2009, 05:38 AM   #1
baidym
LQ Newbie
 
Registered: Oct 2007
Posts: 16

Rep: Reputation: 0
grep -v and -C options


Hi guys,

I'm trying to get grep to exclude three lines in a search string

data-time
data-time
----------- <- to be removed
bad line <- to be removed
----------- <- to be removed
data-time
data-time

I have been unable to get grep to find lines with more than one "-" using '[--]' (I may not be using it correctly, does it need '[\-\-]' or something?)

OR can I use -v AND -C together, it doesnt seem to work:
e.g.
egrep -v -C1 'bad' file > outfile

any ideas ?

M.x.
 
Old 01-02-2009, 06:30 AM   #2
greeneagle
Member
 
Registered: Dec 2004
Location: Dublin Ireland via Cobh, Limerick.
Distribution: RedHat, Suse, Ubuntu.
Posts: 33

Rep: Reputation: 16
egrep -v 'bad|^----' file

-C and -v
-C doesn't work with inverse grep (-v) and stop printing of lines. -C prints lines of context around MATCHED lines only. It would be handy though sometimes if -C and -v worked together, wouldn't it!? Some perl might do the job more neatly.

grepping for -
grepping (or inverse grepping) for "-" might be a problem if you have "-" at start of your argument. Even if quoted it will be parsed as a config argument. You can slashify it or cheat ... e.g. these should be okay:
egrep -v '\-----|bad' file
egrep -v '[-]----|bad' file
not okay:
egrep -v '-----|bad' file
 
Old 01-02-2009, 07:11 AM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
greeneagle: You can also use -e to grep to indicate that the next argument is a pattern no matter what; that way leading dashes won't be treated as the start of an option. Example: egrep -v -e "-----|bad" file

baidym: It looks like what you're trying to do is a little beyond the behaviour of grep that I'm familiar with; deciding whether or not to print the current line based on previous lines is a more complicated operation. You could use Awk like so:
Code:
BEGIN { show = 1; }
/--+/ {
    show = !show;
}
$0 !~ /--+/ {
    if (show) { print $0; }
}
 
Old 01-03-2009, 08:59 AM   #4
dhanyaelizabeth
LQ Newbie
 
Registered: Jan 2009
Posts: 27

Rep: Reputation: 15
On a different note, I have a question regarding grep.

On one of my exployer's server - I notice that I can do a
grep -ri "search_string" folder_name/

and this does a recursive search inside the folders and subfolders of "folder_name"


But, on another one of their servers - when I issue a grep -ri "search_string" folder_name/
I get a "type grep -help for more options" screen.


Am i missing something here ?

Linux Archive

Last edited by dhanyaelizabeth; 01-10-2009 at 03:14 PM.
 
Old 01-03-2009, 02:47 PM   #5
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Grep is a tool with a long history (first appearing in Version 6 AT&T UNIX according to my man page) and is covered by POSIX. Implementations from different vendors vary in the options they provide. Not sure what systems you were using, but I can tell you from experience that GNU grep and BSD grep differ, sometimes subtly. With regards to recursive searching, the manpage for grep on OpenBSD indicates that recursive searching is activated with -R but that -r is provided for compatibility with other versions of grep. The manpage also states that the recursive option specified by POSIX is -R and that no option -r is specified by POSIX. It also recommends against using -r even though it is provided, probably since -R does the same thing and is more portable.
 
  


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
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
I need grep options to search inside a .tar.gz file. ZAMO Linux - General 2 06-24-2008 11:55 PM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 07:12 AM
Kernel 2.6.2 options question - LOCKED options ? tvojvodi Linux - General 0 02-17-2004 04:23 AM
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 03:28 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