LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   grep -v and -C options (https://www.linuxquestions.org/questions/programming-9/grep-v-and-c-options-694437/)

baidym 01-02-2009 05:38 AM

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.

greeneagle 01-02-2009 06:30 AM

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

taylor_venable 01-02-2009 07:11 AM

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; }
}


dhanyaelizabeth 01-03-2009 08:59 AM

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

taylor_venable 01-03-2009 02:47 PM

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.


All times are GMT -5. The time now is 10:43 PM.