Extract Lines From Text File

Home » CentOS » Extract Lines From Text File
CentOS 15 Comments


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.

Any ideas.



15 thoughts on - Extract Lines From Text File

  • Em 15-08-2012 11:22, Tony Molloy escreveu:

    ]$ cat testCentOSlist m9718508:pw:9301:840: Lynch :/home/pgstud/m9718508:/bin/bash m9718308:pw:9301:850: Lynch :/home/pgstud/m9718508:/bin/bash m9718208:pw:9301:840: Lynch :/home/pgstud/m9718508:/bin/bash m9718508:pw:9301:840: Lynch :/home/pgstud/m9718908:/bin/bash

    ]$ cat testCentOSlist | egrep ^m.*:.*:.*:850:
    m9718308:pw:9301:850: Lynch :/home/pgstud/m9718508:/bin/bash

  • wwp wrote:


    awk ‘{ if ($1 ~ /^m/ && $4 == “850” ) { print $0;}}’ /etc/passwd

    mark “awk! awk!*”

    * No, I’m still not a seagull….

  • In article <81eb30fb297893749f5c1e211f08c7e4.squirrel@mail.5-cent.us>,

    awk -F: ‘{ if ($1 ~ /^m/ && $4 == “850” ) { print $0;}}’ /etc/passwd

    Cheers Tony

  • 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.



  • Tony Molloy wrote:
    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


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

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

    ^ ^ Mark LaPierre Registerd Linux user No #267004

  • No these are 4th year graduated students. If they stay on as post graduate students they have to re-register. So clean them out I say


  • Hello m.roth@5-cent.us,

    I found that regexp particularly simple :-D. Not here to troll, but simple or complicated, I think it’s only about a bit of knowledge
    (understanding), a learning curve. Personally I don’t feel comfortable w/ awk expressions/language. Not saying that one if better than the other – I would rather think that knowing both is way better. Don’t be afraid with regular expressions, there’s nothing really complex about most of them!