Path: csiph.com!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.unix.misc Subject: Re: How do you "grep" in a string when you know how it starts and how it ends? Date: Wed, 24 Apr 2024 08:58:02 -0700 Organization: None to speak of Lines: 52 Message-ID: <87a5lin46t.fsf@nosuchdomain.example.com> References: <87edaun4fh.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Wed, 24 Apr 2024 17:58:03 +0200 (CEST) Injection-Info: dont-email.me; posting-host="8912a4ab33884690e874a0120806b9cc"; logging-data="2534126"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX192O72dTVKlLFUsdEg3Mbyl" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:VMiT3Erh9VTEsYaScakl53zFNxM= sha1:sED+4sKaflVLMp2i5OT7bWp6dsI= Xref: csiph.com comp.unix.misc:442 Keith Thompson writes: > Ottavio Caruso 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 */