FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > CentOS > CentOS

 
 
LinkBack Thread Tools
 
Old 08-15-2012, 03:47 PM
 
Default Extract lines from text file

wwp wrote:
> On Wed, 15 Aug 2012 15:22:10 +0100 Tony Molloy <tony.molloy@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.
>>
>> m9718508w: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

mark "awk! awk!*"

* No, I'm still not a seagull....

_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 08-15-2012, 03:58 PM
 
Default Extract lines from text file

In article <81eb30fb297893749f5c1e211f08c7e4.squirrel@mail. 5-cent.us>,
<m.roth@5-cent.us> wrote:
> wwp wrote:
> > On Wed, 15 Aug 2012 15:22:10 +0100 Tony Molloy <tony.molloy@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.
> >>
> >> m9718508w: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

Cheers
Tony
--
Tony Mountifield
Work: tony@softins.co.uk - http://www.softins.co.uk
Play: tony@mountifield.org - http://tony.mountifield.org
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 08-15-2012, 05:22 PM
 
Default Extract lines from text file

Tony Mountifield wrote:
> In article <81eb30fb297893749f5c1e211f08c7e4.squirrel@mail. 5-cent.us>,
> <m.roth@5-cent.us> wrote:
>> wwp wrote:
>> > On Wed, 15 Aug 2012 15:22:10 +0100 Tony Molloy <tony.molloy@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.
>> >>
>> >> m9718508w: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

_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 08-15-2012, 05:45 PM
Tilman Schmidt
 
Default Extract lines from text file

Am 15.08.2012 16:36, schrieb Marcelo Beckmann:
> Em 15-08-2012 11:22, Tony Molloy escreveu:
[...]
>> cat passwdfile | grep ^m | grep 850 > output
[...]
> ]$ cat testcentoslist | egrep ^m.*:.*:.*:850:

http://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat

Because a cat is a terrible thing to waste.

SCNR,
T.
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 08-15-2012, 06:02 PM
 
Default Extract lines from text file

Tilman Schmidt wrote:
> Am 15.08.2012 16:36, schrieb Marcelo Beckmann:
>> Em 15-08-2012 11:22, Tony Molloy escreveu:
> [...]
>>> cat passwdfile | grep ^m | grep 850 > output
> [...]
>> ]$ cat testcentoslist | egrep ^m.*:.*:.*:850:
>
> http://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat
>
> Because a cat is a terrible thing to waste.
>
And why would you want to disturb our Lords & Masters? I mean, why are
*we* here?

mark

--
The truth is out, we know at last:
Dogs have masters, cats have staff.

_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 08-15-2012, 06:26 PM
Tony Molloy
 
Default Extract lines from text file

On Wednesday 15 August 2012 15:36:09 Marcelo Beckmann wrote:
> Em 15-08-2012 11:22, Tony Molloy escreveu:
> > Hi,
> >
> > 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.
> >
> > m9718508w: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.
> >
> > Any ideas.
>
> ]$ cat testcentoslist
> m9718508w:9301:840: Lynch :/home/pgstud/m9718508:/bin/bash
> m9718308w:9301:850: Lynch :/home/pgstud/m9718508:/bin/bash
> m9718208w:9301:840: Lynch :/home/pgstud/m9718508:/bin/bash
> m9718508w:9301:840: Lynch :/home/pgstud/m9718908:/bin/bash
>
> ]$ cat testcentoslist | egrep ^m.*:.*:.*:850:
> m9718308w:9301:850: Lynch :/home/pgstud/m9718508:/bin/bash
>

Exactly what I needed. I'll just drop the cat as a later poster
pointed out.

Thanks to all who replied.

Tony
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 08-15-2012, 06:46 PM
Les Mikesell
 
Default Extract lines from text file

On Wed, Aug 15, 2012 at 1:26 PM, Tony Molloy <tony.molloy@ul.ie> wrote:
>>>
>> ]$ cat testcentoslist | egrep ^m.*:.*:.*:850:
>> m9718308w:9301:850: Lynch :/home/pgstud/m9718508:/bin/bash
>>
>
> Exactly what I needed. I'll just drop the cat as a later poster
> pointed out.
>

sed -n -e '/pattern/p' can match anything grep would do and might be
even more useful if you want substitutions for subsequent use. And of
course perl can do anything sed can do, and then some...

--
Les Mikesell
lesmikesell@gmail.com
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 08-15-2012, 07:40 PM
Tony Molloy
 
Default Extract lines from text file

On Wednesday 15 August 2012 19:46:42 Les Mikesell wrote:
> On Wed, Aug 15, 2012 at 1:26 PM, Tony Molloy <tony.molloy@ul.ie>
wrote:
> >> ]$ cat testcentoslist | egrep ^m.*:.*:.*:850:
> >> m9718308w:9301:850: Lynch :/home/pgstud/m9718508:/bin/bash
> >
> > Exactly what I needed. I'll just drop the cat as a later poster
> > pointed out.
>
> sed -n -e '/pattern/p' can match anything grep would do and might
> be even more useful if you want substitutions for subsequent use.
> And of course perl can do anything sed can do, and then some...
>

True true, and of course C could do anything and everything ;-)

But all I need is a simple script which will be run once a year to
remove the graduated students from the password file.

Regards,

Tony
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 08-15-2012, 07:53 PM
 
Default Extract lines from text file

Tony Molloy wrote:
> On Wednesday 15 August 2012 19:46:42 Les Mikesell wrote:
>> On Wed, Aug 15, 2012 at 1:26 PM, Tony Molloy <tony.molloy@ul.ie>
> wrote:
>> >> ]$ cat testcentoslist | egrep ^m.*:.*:.*:850:
>> >> m9718308w:9301:850: Lynch :/home/pgstud/m9718508:/bin/bash
>> >
>> > Exactly what I needed. I'll just drop the cat as a later poster
>> > pointed out.
>>
>> sed -n -e '/pattern/p' can match anything grep would do and might
>> be even more useful if you want substitutions for subsequent use.
>> And of course perl can do anything sed can do, and then some...
>>
>
> True true, and of course C could do anything and everything ;-)
>
> But all I need is a simple script which will be run once a year to
> remove the graduated students from the password file.
>
Ah, but are you sure they're not just dropped out for a term, or about to
become indentured servants, er, grad students? In that case, maybe just
change their login shell to /bin/noLogin

mark



_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 08-16-2012, 12:10 AM
Mark LaPierre
 
Default Extract lines from text file

On 08/15/2012 10:22 AM, Tony Molloy wrote:
>
> Hi,
>
> 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.
>
> m9718508w: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.
>
> Any ideas.
>
> Thanks,
>
> Tony
> _______________________________________________
> CentOS mailing list
> CentOS@centos.org
> http://lists.centos.org/mailman/listinfo/centos
>

[mlapier@mushroom ~]$ cat tmpfile
m9718508w:9301:840: Lynch :/home/pgstud/m9718508:/bin/bash
m1234567w:9302:850: Lynch :/home/pgstud/m1234567:/bin/bash

[mlapier@mushroom ~]$ grep ^m tmpfile | grep ":850:"
m1234567w:9302:850: Lynch :/home/pgstud/m1234567:/bin/bash
[mlapier@mushroom ~]$

--
_
v
/(_)
^ ^ Mark LaPierre
Registerd Linux user No #267004
www.counter.li.org
****
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 

Thread Tools




All times are GMT. The time now is 02:11 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org