Groups | Search | Server Info | Login | Register


Groups > comp.unix.misc > #442

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

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.unix.misc
Subject Re: How do you "grep" in a string when you know how it starts and how it ends?
Date 2024-04-24 08:58 -0700
Organization None to speak of
Message-ID <87a5lin46t.fsf@nosuchdomain.example.com> (permalink)
References <v0b809$2cs3r$1@dont-email.me> <87edaun4fh.fsf@nosuchdomain.example.com>

Show all headers | View raw


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 */

Back to comp.unix.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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

csiph-web