Unix Shell - simple grep -v not working

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2005 > simple grep -v not working





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author simple grep -v not working
RolandRB

2005-02-21, 7:54 am

I have no idea whay tis isn't working and have no time to spend
investigating. Please tell me why this isn't working. I want to display
lines that end in a colon but not if they start with a slash-space. But
they get through when I use the following.

grep ': *$' /data/null /data/null "$@" | grep -v '^/ '

Anthony Borla

2005-02-21, 7:54 am

"RolandRB" <rolandberry@hotmail.com> wrote in message
news:1108981421.604987.89420@f14g2000cwb.googlegroups.com...
> I have no idea whay tis isn't working and have no time to
> spend investigating. Please tell me why this isn't working.
> I want to display lines that end in a colon but not if they start
> with a slash-space. But they get through when I use the
> following.
>
> grep ': *$' /data/null /data/null "$@" | grep -v '^/ '
>


The following, I believe, should do the trick:

grep '^[^/][^ ].*:$' filespec

I hope this helps.

Anthony Borla


Bruce Barnett

2005-02-21, 7:54 am

"RolandRB" <rolandberry@hotmail.com> writes:

> I have no idea whay tis isn't working and have no time to spend
> investigating. Please tell me why this isn't working. I want to display
> lines that end in a colon but not if they start with a slash-space. But
> they get through when I use the following.
>
> grep ': *$' /data/null /data/null "$@" | grep -v '^/ '
>


/data/null? Don't you meant /dev/null?


And when you specify '^/ '
this matches
"/" as first characer of line
" " as second character on line.

So I don't think this is what you want.

And because you list more than one filename, you may likely get '/'
because the filename that grep prepends has a "/"

Perhaps you want

cat "$@" | grep ': *$' | grep -v '^/'




--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.
Eric Moors

2005-02-21, 7:54 am

RolandRB wrote:

> I have no idea whay tis isn't working and have no time to spend
> investigating. Please tell me why this isn't working. I want to display
> lines that end in a colon but not if they start with a slash-space. But
> they get through when I use the following.
>
> grep ': *$' /data/null /data/null "$@" | grep -v '^/ '


No they don't come through.
But "/<tab>.*" does, as does "/<any non-printable>.*

try grep -v '^/[[:space:]]'

Eric
Bill Marcum

2005-02-21, 7:54 am

On 21 Feb 2005 02:23:41 -0800, RolandRB
<rolandberry@hotmail.com> wrote:
> I have no idea whay tis isn't working and have no time to spend
> investigating. Please tell me why this isn't working. I want to display
> lines that end in a colon but not if they start with a slash-space. But
> they get through when I use the following.
>
> grep ': *$' /data/null /data/null "$@" | grep -v '^/ '
>

Is it possible that some lines begin with slash-tab? Or invisible
control characters before or after the slash?
Yomesh Shah

2005-02-22, 2:52 am

"RolandRB" <rolandberry@hotmail.com> wrote in message news:<1108981421.604987.89420@f14g2000cwb.googlegroups.com>...
> I have no idea whay tis isn't working and have no time to spend
> investigating. Please tell me why this isn't working. I want to display
> lines that end in a colon but not if they start with a slash-space. But
> they get through when I use the following.
>
> grep ': *$' /data/null /data/null "$@" | grep -v '^/ '


the / is a regular expression , just use '' to make grep read the '/'
as the character instead of it unix meaning.
the following command will work,

grep ': *$' /data/null /data/null "$@" | grep -v '^\/'

Also check for the trailing space after the / , if u need it or is it a typo.

regds

Yomesh
Bill Seivert

2005-02-22, 2:52 am



RolandRB wrote:
> I have no idea whay tis isn't working and have no time to spend
> investigating. Please tell me why this isn't working. I want to display
> lines that end in a colon but not if they start with a slash-space. But
> they get through when I use the following.
>
> grep ': *$' /data/null /data/null "$@" | grep -v '^/ '
>


First, should /data/null be /dev/null ? Second, it only needs to be
specified once, not twice. Since $@ is being used, one can assume
that there may be multiple filenames passed into this statement.
When grep gets multiple filenames on the command line, the standard
action is to prepend each line found with the filename in which the
line was found, followed by a colon, then the first character of
the line.

Third, the first grep should probably also consider trailing tabs,
so use grep ':[ ]*$' (that's one space and one tab between
brackets).

Fourth, the second grep knows nothing of whether the first grep is
displaying filenames, so looking for a slash-space (/ ) anchored
to the beginning of the line will almost surely fail, unless you
have a file in the root directory that begins with a space.

Two suggestions:

grep '^[^/][^ ].*:[ ]*$' $@ /dev/null

(though this will miss a lines with a single colon, a slash-colon
or a space-colon.)

or

grep ':[ ]*$' $@ /dev/null | grep -v '^[^:][^:]*:/ '

The second grep in the second statement says skip the line (-v)
if the leading filename and its trailing colon are immediately
followed by a slash and a space. Note, this will only work for
filenames that do not contain embedded colons.

And, no, the slash is not a metacharacter for regular expressions
so it does not need to be escaped.

Bill Seivert

Barry Margolin

2005-02-22, 2:52 am

In article <8421f034.0502211906.4d6ab3fc@posting.google.com>,
yomesh@gmail.com (Yomesh Shah) wrote:

> "RolandRB" <rolandberry@hotmail.com> wrote in message
> news:<1108981421.604987.89420@f14g2000cwb.googlegroups.com>...
>
> the / is a regular expression , just use '' to make grep read the '/'
> as the character instead of it unix meaning.


Since when does / have special meaning in regular expressions? What's
its special meaning?

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Eric Moors

2005-02-24, 5:58 pm

RolandRB wrote:

> I have no idea whay tis isn't working and have no time to spend
> investigating. Please tell me why this isn't working. I want to display
> lines that end in a colon but not if they start with a slash-space. But
> they get through when I use the following.
>
> grep ': *$' /data/null /data/null "$@" | grep -v '^/ '


No they don't come through.
But "/<tab>.*" does, as does "/<any non-printable>.*

try grep -v '^/[[:space:]]'

Eric
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com