[CentOS] Extract lines from text file

m.roth at 5-cent.us m.roth at 5-cent.us
Wed Aug 15 17:22:33 UTC 2012


Tony Mountifield wrote:
> In article <81eb30fb297893749f5c1e211f08c7e4.squirrel at mail.5-cent.us>,
>  <m.roth at 5-cent.us> wrote:
>> wwp wrote:
>> > On Wed, 15 Aug 2012 15:22:10 +0100 Tony Molloy <tony.molloy at ul.ie>
>> wrote:
>> >>
>> >> I'm looking for a command to extract lines from a large text file, a
>> >> password file. A typical user has a username made from a letter
>> >> followed by their id-number.
>> >>
>> >> m9718508:pw:9301:840: Lynch  :/home/pgstud/m9718508:/bin/bash
>> >>
>> >> So for instance if I need to extract lines where;
>> >> the 1st field, the username begins with an m
>> >> and the 4th field, the group contains exactly 850
>> >>
>> >> cat passwdfile | grep ^m | grep 850 > output
>> >>
>> >> is close but fails if the value 850 appears outside the 4th field. In
>> >> the above example which should be ignored 850 appears in the username
>> >> and home directory and is therefore extracted.
>> >
>> > Something like `grep -E '^m.+:.*:.*:850:'` maybe?
>>
>> Complicated.
>>
>> awk '{ if ($1 ~ /^m/ && $4 == "850" ) { print $0;}}' /etc/passwd
>
> awk -F: '{ if ($1 ~ /^m/ && $4 == "850" ) { print $0;}}' /etc/passwd

Or
awk 'BEGIN { FS=":";}{ if ($1 ~ /^m/ && $4 == "850" ) { print $0;}}'
/etc/passwd

        mark




More information about the CentOS mailing list