Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.unix.misc > #440 > unrolled thread

How do you "grep" in a string when you know how it starts and how it ends?

Started byOttavio Caruso <ottavio2006-usenet2012@yahoo.com>
First post2024-04-24 16:20 +0100
Last post2024-07-11 23:25 +1300
Articles 5 — 4 participants

Back to article view | Back to comp.unix.misc


Contents

  How do you "grep" in a string when you know how it starts and how it ends? Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> - 2024-04-24 16:20 +0100
    Re: How do you "grep" in a string when you know how it starts and how it ends? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-24 08:52 -0700
      Re: How do you "grep" in a string when you know how it starts and how it ends? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-24 08:58 -0700
    Re: How do you "grep" in a string when you know how it starts and how it ends? Grant Taylor <gtaylor@tnetconsulting.net> - 2024-04-24 20:19 -0500
      How do you "grep" in a string when you know how it starts and how i Amessyroom@f10.n1.z38.fidonet.org (Amessyroom) - 2024-07-11 23:25 +1300

#440 — How do you "grep" in a string when you know how it starts and how it ends?

FromOttavio Caruso <ottavio2006-usenet2012@yahoo.com>
Date2024-04-24 16:20 +0100
SubjectHow do you "grep" in a string when you know how it starts and how it ends?
Message-ID<v0b809$2cs3r$1@dont-email.me>
Sorry for the convoluted title, but let's say I am looking for all the 
occurrences of "keep on *ing" in a file, so that it catches:

keep on running
keep on doing
keep on walking


I tried:

$ grep -i "keep on *ing" file

and

$ grep -i "keep on [a-zA-Z0-9_]ing" file

but it obviously didn't work.

Thanks

-- 
Ottavio Caruso

[toc] | [next] | [standalone]


#441

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2024-04-24 08:52 -0700
Message-ID<87edaun4fh.fsf@nosuchdomain.example.com>
In reply to#440
Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> writes:
> Sorry for the convoluted title, but let's say I am looking for all the
> occurrences of "keep on *ing" in a file, so that it catches:
>
> keep on running
> keep on doing
> keep on walking
>
>
> I tried:
>
> $ grep -i "keep on *ing" file

The " *" matches zero or more spaces.

> and
>
> $ grep -i "keep on [a-zA-Z0-9_]ing" file

The "[a-zA-Z0-9_]" matches exactly one character from the set, so it
will match "keep on xing".

> but it obviously didn't work.

$ grep -i "keep on [a-zA-Z0-9_]*ing" file

That will match "keep on ing".  If you want to match at least one
alphanumeric before the "ing:

$ grep -E -i "keep on [a-zA-Z0-9_]+ing" file

This doesn't handle accented letters.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#442

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2024-04-24 08:58 -0700
Message-ID<87a5lin46t.fsf@nosuchdomain.example.com>
In reply to#441
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
> Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> writes:
>> Sorry for the convoluted title, but let's say I am looking for all the
>> occurrences of "keep on *ing" in a file, so that it catches:
>>
>> keep on running
>> keep on doing
>> keep on walking
>>
>>
>> I tried:
>>
>> $ grep -i "keep on *ing" file
>
> The " *" matches zero or more spaces.
>
>> and
>>
>> $ grep -i "keep on [a-zA-Z0-9_]ing" file
>
> The "[a-zA-Z0-9_]" matches exactly one character from the set, so it
> will match "keep on xing".
>
>> but it obviously didn't work.
>
> $ grep -i "keep on [a-zA-Z0-9_]*ing" file
>
> That will match "keep on ing".  If you want to match at least one
> alphanumeric before the "ing:
>
> $ grep -E -i "keep on [a-zA-Z0-9_]+ing" file
>
> This doesn't handle accented letters.

Something that can be confusing is that file matching patterns and
regular expressions are different.  In a file matching pattern, "*"
matches zero or more arbitrary characters:

    ls foo*.txt

In a regular expression, "*" matches zero or more occurrences of
whatever precedes it, and "." matches a single character:

    ls | grep '^foo.*\.txt$'

The above two commands are nearly equivalent (there could be some
differences depending on how "ls" displays unusual characters).

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#443

FromGrant Taylor <gtaylor@tnetconsulting.net>
Date2024-04-24 20:19 -0500
Message-ID<v0cb3j$kht$1@tncsrv09.home.tnetconsulting.net>
In reply to#440
On 4/24/24 10:20, Ottavio Caruso wrote:
> Sorry for the convoluted title, but let's say I am looking for all the 
> occurrences of "keep on *ing" in a file, so that it catches:

grep -i "keep on " file | grep "ing"

or

egrep -i "keep on .*ing" file



-- 
Grant. . . .

[toc] | [prev] | [next] | [standalone]


#444 — How do you "grep" in a string when you know how it starts and how i

FromAmessyroom@f10.n1.z38.fidonet.org (Amessyroom)
Date2024-07-11 23:25 +1300
SubjectHow do you "grep" in a string when you know how it starts and how i
Message-ID<720754781@f10.n1.z38.fidonet.org>
In reply to#443
  Re: How do you "grep" in a string when you know how it starts and how i
  By: Grant Taylor to Ottavio Caruso on Wed Apr 24 2024 08:19 pm

 > On 4/24/24 10:20, Ottavio Caruso wrote:
 > > Sorry for the convoluted title, but let's say I am looking for all the
 > > occurrences of "keep on *ing" in a file, so that it catches:
 > 
 > grep -i "keep on " file | grep "ing"
 > 
 > or
 > 
 > egrep -i "keep on .*ing" file
 > 
 > 
 > 
 > --
 > Grant. . . .
Glad to see there is some "shell" discussion in this group. Just started 
 pulling this usenet group into my bbs; and seem to be a lot of SPAM before 
this.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.unix.misc


csiph-web