bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Uniq pattern


From: Greg Wooledge
Subject: Re: Uniq pattern
Date: Mon, 4 Jan 2016 08:11:51 -0500
User-agent: Mutt/1.4.2.3i

On Thu, Dec 31, 2015 at 06:44:07PM -0600, John McKown wrote:
> On Thu, Dec 31, 2015 at 6:15 PM, Val Krem <valkrem@yahoo.com> wrote:
> > I wanted to search a line  that contains  "1235"  using
> > grep   -R "1235".
> > I am getting  unwanted results such as
> >  01235 or 12356 and so on.
> >
> > How could I avoid these and get only '1235"?
> 
> I don't know what you mean by "only 1235". Do you mean with no leading or
> trailing digits? I.e. A1235B is a match, but 01235 is not. Or do you mean
> "a line which contains _only_ 1235 and no other characters?
> 
> For the first, this should work: egrep
> '(^|[^[:digit:]])1235($|[^[:digit:]])'
> 
> ({^|[^[:digit:])) matches the beginning of the ???string (^) or a non-digit
> [^[:digit:]]
> then the digits "1235
> ($|[^[:digit:]]) then matches the end of the string ($) or a non-digit.

Those are possible interpretations, but I can think of others.

"A line containing only 1235" is the easiest case, so let's get that one
out of the way:

  grep -Fx 1235

"Lines containing 1235 as a space-delimited word" is the more likely
question.  That one is "easier" if you are using GNU grep with its
perl-ish extensions.  But let's assume POSIX grep.  The word 1235
can appear with spaces on both sides, or it can be at the start of
the line (with or without trailing spaces), or it can be at the
end of the line (with or without leading spaces).

  grep -E '(^|[[:space:]])1235($|[[:space:]])'

That matches 1235 with either start-of-line or a space before it,
and with either end-of-line or a space after it.

For Val, please be as clear as you can possibly be with your questions.
It's best to provide example inputs, especially tricky or subtle examples
that clearly draw the line between what you want and what you don't want.

> Oh, what does this have to do with BASH????? And how is it a bug? IOW, this
> looks like the wrong forum. This is not a general UNIX (or Linux) question
> and answer forum. You might want to get an account on StackExchange
> http://stackexchange.com/

Yes and no.  While help-bash@gnu.org is a more appropriate forum for
this kind of question, bug-bash was also used this way for many years.
All we can do is gently steer people toward help-bash.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]