Pattern matching with grep

Pattern matching with grep

Post by Willia » Thu, 02 Jun 2005 19:18:21



Hi all,

How can I use grep to find an exact pattern match, for example if I were to
grep the first field of the /etc/passwd file for "m",

awk -F: '{print $}' /etc/passwd | grep m

I get every userid that contains "m" in the passwd file, I'd like to
matching any occurrence of only "m" in this case?

--
Regards

William

 
 
 

Pattern matching with grep

Post by laurentschnei.. » Thu, 02 Jun 2005 19:20:50


grep ^m: /etc/passwd

 
 
 

Pattern matching with grep

Post by Willia » Thu, 02 Jun 2005 19:25:50



> grep ^m: /etc/passwd

Thanks! what does the ^ and : symbols mean in the string?

--
Regards

William

 
 
 

Pattern matching with grep

Post by Willia » Thu, 02 Jun 2005 19:29:53




> > grep ^m: /etc/passwd

> Thanks! what does the ^ and : symbols mean in the string?

> --
> Regards

> William

If I try to match a numeric string using grep ^14: /etc/passwd this fails to
find anything, is there a different format for numeric strings?

--
Regards

William

 
 
 

Pattern matching with grep

Post by Chris F.A. Johnso » Thu, 02 Jun 2005 19:43:05





>> > grep ^m: /etc/passwd

>> Thanks! what does the ^ and : symbols mean in the string?

> If I try to match a numeric string using grep ^14: /etc/passwd this fails to
> find anything, is there a different format for numeric strings?

    The caret (^) anchors the search to the beginning of the line. The
    colon just matches a colon. Unless you have a user with the name
    "14", that command will not find a match.

--
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>

 
 
 

Pattern matching with grep

Post by Willia » Thu, 02 Jun 2005 19:51:09





> >> > grep ^m: /etc/passwd

> >> Thanks! what does the ^ and : symbols mean in the string?

> > If I try to match a numeric string using grep ^14: /etc/passwd this
fails to
> > find anything, is there a different format for numeric strings?

>     The caret (^) anchors the search to the beginning of the line. The
>     colon just matches a colon. Unless you have a user with the name
>     "14", that command will not find a match.

> --
>     Chris F.A. Johnson                     <http://cfaj.freeshell.org>
>     ==================================================================
>     Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
>     <http://www.torfree.net/~chris/books/cfaj/ssr.html>

Thanks Chris,

I'm also looking to find unique occurrences on group id's, for example if I
grep for GID 100, I find 1001, 1002 etc, however GID 100 is not there.. is
there a similar pattern match for numeric strings?

Regards

William

 
 
 

Pattern matching with grep

Post by Loki Harfag » Thu, 02 Jun 2005 20:07:37


Le Wed, 01 Jun 2005 10:51:09 +0000, William a crit?:




>> >> > grep ^m: /etc/passwd

>> >> Thanks! what does the ^ and : symbols mean in the string?

>> > If I try to match a numeric string using grep ^14: /etc/passwd this
> fails to
>> > find anything, is there a different format for numeric strings?


>>     The caret (^) anchors the search to the beginning of the line. The
>>     colon just matches a colon. Unless you have a user with the name
>>     "14", that command will not find a match.

>> --
>>     Chris F.A. Johnson                     <http://cfaj.freeshell.org>
>>     ==================================================================
>>     Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
>>     <http://www.torfree.net/~chris/books/cfaj/ssr.html>

> Thanks Chris,

> I'm also looking to find unique occurrences on group id's, for example if I
> grep for GID 100, I find 1001, 1002 etc, however GID 100 is not there..

Well, if no group has the value 100 it's not our fault !
Maybe you have no users #:D)
Try with :

$ grep :100: /etc/group
$ grep :100: /etc/passwd

if it works you made a typo in the grep you tried afore :-)

Quote:> is
> there a similar pattern match for numeric strings?

You're still quite confused (and confusing ;-) may I
suggest you to read som man pages and google a bit on regexp ?-D)

It is not to avoid answering to you but it should help you
having better questions ;-)

 
 
 

Pattern matching with grep

Post by Chris F.A. Johnso » Thu, 02 Jun 2005 20:10:29






>> >> > grep ^m: /etc/passwd

>> >> Thanks! what does the ^ and : symbols mean in the string?

>> > If I try to match a numeric string using grep ^14: /etc/passwd
>> > this fails to find anything, is there a different format for
>> > numeric strings?


>>     The caret (^) anchors the search to the beginning of the line. The
>>     colon just matches a colon. Unless you have a user with the name
>>     "14", that command will not find a match.

> Thanks Chris,

> I'm also looking to find unique occurrences on group id's, for example if I
> grep for GID 100, I find 1001, 1002 etc, however GID 100 is not there.. is
> there a similar pattern match for numeric strings?

grep '^[^:]*:[^:]*:[^:]*:100:' /etc/passwd

  "[^:]* == zero or more occurences of any character except ":"

  Or you could use awk:

gid=100
awk -F: '$4 == gid {print}' gid=$gid /etc/passwd

  ({print} is not necessary, but I prefer to include it as it makes
  the code clearer and future modifications easier.)

--
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>

 
 
 

Pattern matching with grep

Post by Willia » Thu, 02 Jun 2005 20:12:00



> Le Wed, 01 Jun 2005 10:51:09 +0000, William a crit :




> >> >> > grep ^m: /etc/passwd

> >> >> Thanks! what does the ^ and : symbols mean in the string?

> >> > If I try to match a numeric string using grep ^14: /etc/passwd this
> > fails to
> >> > find anything, is there a different format for numeric strings?


> >>     The caret (^) anchors the search to the beginning of the line. The
> >>     colon just matches a colon. Unless you have a user with the name
> >>     "14", that command will not find a match.

> >> --
> >>     Chris F.A. Johnson                     <http://cfaj.freeshell.org>
> >>     ==================================================================
> >>     Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
> >>     <http://www.torfree.net/~chris/books/cfaj/ssr.html>

> > Thanks Chris,

> > I'm also looking to find unique occurrences on group id's, for example
if I
> > grep for GID 100, I find 1001, 1002 etc, however GID 100 is not there..

> Well, if no group has the value 100 it's not our fault !
> Maybe you have no users #:D)
> Try with :

> $ grep :100: /etc/group
> $ grep :100: /etc/passwd

> if it works you made a typo in the grep you tried afore :-)

> > is
> > there a similar pattern match for numeric strings?

> You're still quite confused (and confusing ;-) may I
> suggest you to read som man pages and google a bit on regexp ?-D)

> It is not to avoid answering to you but it should help you
> having better questions ;-)

I get it now :-)

I'll have some more daft questions later, don't worry :-)

Thanks

William

 
 
 

Pattern matching with grep

Post by Sven Maschec » Thu, 02 Jun 2005 20:31:20



> grep ^m: /etc/passwd

(Note that an unescaped ^ works like | in traditional Bourne shells.)
 
 
 

Pattern matching with grep

Post by Willia » Thu, 02 Jun 2005 21:24:31



> > grep ^m: /etc/passwd

> (Note that an unescaped ^ works like | in traditional Bourne shells.)

Thanks all for your help.

--
Regards

William

 
 
 

Pattern matching with grep

Post by Ed Morto » Thu, 02 Jun 2005 21:33:44



> Hi all,

> How can I use grep to find an exact pattern match, for example if I were to
> grep the first field of the /etc/passwd file for "m",

> awk -F: '{print $}' /etc/passwd | grep m

> I get every userid that contains "m" in the passwd file, I'd like to
> matching any occurrence of only "m" in this case?

Each line in the password file has this format:

        account:password:UID:GID:GECOS:directory:shell

See http://www.rt.com/man/passwd.5.html for details.

So, there are 7 fields separated by ":"s. Here's examples of how to use
awk to find patterns in them by testing the specific fields $1 through $7:

To find an "account" that CONTAINS "m":

        awk -F: '$1 ~ /m/' /etc/passwd

To find an "account" that STARTS WITH "m":

        awk -F: '$1 ~ /^m/' /etc/passwd

To find an "account" that ENDS WITH "m":

        awk -F: '$1 ~ /m$/' /etc/passwd

To find an "account" that IS EXACTLY "m", either:

        awk -F: '$1 ~ /^m$/' /etc/passwd
        awk -F: '$1 == "m"' /etc/passwd

To find a "GID" that IS EXACTLY "100", either:

        awk -F: '$4 ~ /^100$/' /etc/passwd
        awk -F: '$4 == "100"' /etc/passwd

And so on...

Hope that helps,

        Ed.

 
 
 

Pattern matching with grep

Post by Willia » Thu, 02 Jun 2005 21:56:01




> > Hi all,

> > How can I use grep to find an exact pattern match, for example if I were
to
> > grep the first field of the /etc/passwd file for "m",

> > awk -F: '{print $}' /etc/passwd | grep m

> > I get every userid that contains "m" in the passwd file, I'd like to
> > matching any occurrence of only "m" in this case?

> Each line in the password file has this format:

> account:password:UID:GID:GECOS:directory:shell

> See http://www.rt.com/man/passwd.5.html for details.

> So, there are 7 fields separated by ":"s. Here's examples of how to use
> awk to find patterns in them by testing the specific fields $1 through $7:

> To find an "account" that CONTAINS "m":

> awk -F: '$1 ~ /m/' /etc/passwd

> To find an "account" that STARTS WITH "m":

> awk -F: '$1 ~ /^m/' /etc/passwd

> To find an "account" that ENDS WITH "m":

> awk -F: '$1 ~ /m$/' /etc/passwd

> To find an "account" that IS EXACTLY "m", either:

> awk -F: '$1 ~ /^m$/' /etc/passwd
> awk -F: '$1 == "m"' /etc/passwd

> To find a "GID" that IS EXACTLY "100", either:

> awk -F: '$4 ~ /^100$/' /etc/passwd
> awk -F: '$4 == "100"' /etc/passwd

> And so on...

> Hope that helps,

> Ed.

That does Ed, thanks!

Although I've written some scripts in the past, they've been nothing to
complicated and I should now learn more!

--
Regards

William

 
 
 

Pattern matching with grep

Post by laurentschnei.. » Fri, 03 Jun 2005 22:15:39


Quote:> > grep ^m: /etc/passwd
> (Note that an unescaped ^ works like | in traditional Bourne shells.)

Thanks a lot for this correction, I did not know that one !
 
 
 

1. Pattern matching and extracting the data which matches the pattern

Help,

I am running Solaris 2.6, and have only ever written scripts for the Korn
Shell......and have the following query......

I have a very long text file ( each line represents a CAD model name ).
Somewhere within the model name (i.e. line of the text file ) , is  the
drawing reference number......this number will always match a particular
pattern of letters, hyphens and numbers - which will not be matched
elsewhere within the remainder of the line.
I need a mechanism to search each line in turn, match the pattern of
letters, hyphens and numbers and extract purely that portion of the line and
write it to a separate file. My problem , is that the pattern could appear
anywhere within each line and the number of fields can differ ( if it was
always in the same place or had a constant number of fields, I would use
'cut' )
For example, an extract from the input file could be :-

LD100 WEIGHT DAMPER              W7A   22 Z  535210043-A  USE30SE99MVS
99/10/26
LD100 GROMMET  GUIDE  ROD          W5D   12 Z  545180058-B  USE30SE99MAK
99/10/26
LD100 GROMMET  GUIDE  ROD          W5D   22 Z  545180058-B  USE30SE99MAK
99/10/26
LD100 BRACKET-CABLE,SELECT       W7A   12 Z  535210034-C  USE06SE99LJL
99/10/26
LD100 BRACKET-CABLE,SELECT       W7A   22 Z  535210034-C  USE06SE99LJL
99/10/26

And I need to extract the data matching the following pattern: nine
numbers,a hypen and a letter and put it in an output file - thus the output
I would like to see is :-

535210043-A
545180058-B
545180058-B
535210034-C
535210034-C

Many thanks in advance for your help

Mark Hounslow
LDV Limited

2. Reading is much more interesting than TV (0275/1708)

3. How to grep or find a matching pattern that contains serveral lines?

4. Partition magic problem

5. grep multi line pattern matching

6. LINUX Webserver is sometimes unreachable without obvious reason

7. grep the displays matched patterns only

8. Openwin File manager

9. grepping for a pattern match

10. Pattern Matching Not Working When Pattern Assign To A Variable

11. ksh pattern matching when pattern is in a variable

12. Matching Line After Pattern (Pattern Occurs Multiple Times)

13. Matching a pattern in a file and inserting variable string above the line matched?