Hi everybody! Does anyone know of an existing command-line utility that will look through a simple database file (with records separated by some ascii character, or series of characters) for a pattern, then print the entire record to the screen when a record containing a match is found? I've written a little utility in C which does this, and I've been thinking about adding some additional features before offering it to the community. I won't bother if I'm reinventing the wheel however, so if anyone knows if such a thing exists already, please let me know. -- JAY VOLLMER JVOLLMER@VISI.COM TEXT REFS DOUBLEPLUSUNGOOD SELFTHINK VERGING CRIMETHINK IGNORE FULLWISE
On Monday 31 March 2003 03:25, Jay Vollmer wrote:
Hi everybody!
Does anyone know of an existing command-line utility that will look through a simple database file (with records separated by some ascii character, or series of characters) for a pattern, then print the entire record to the screen when a record containing a match is found?
sed?!
On Mon, 31 Mar 2003 11:51 am, Anders Johansson wrote:
Does anyone know of an existing command-line utility that will look through a simple database file (with records separated by some ascii character, or series of characters) for a pattern, then print the entire record to the screen when a record containing a match is found?
In order of increasing power/complexity: grep sed awk perl Take a standard unix password file, the separator is ":" To find the user who's username is "michael" grep "^michael:" /etc/passwd username is between start-of-line and the first : To find the user with UID of 1000 grep ":1000:" /etc/passwd Note this finds all fields with a value of 1000 so will also return everybody in group 1000 if there is one. It won't give useful results if the value you want occurs lots in other fields. To actually get individual record based tests, awk will do it but I jump straight to perl. perl -naF: -e'$F[0] eq "michaelj" and print;' /etc/passwd $F[0] is the first field of your record, $F[1] the second, etc... You have the full perl bestiary of regular expressions to spot the value you want. For that matter, the field seperator ":" could have been a regexp. And by playing with the -0 switch (or $/ variable), multi-line records can be parsed. -- Michael James michael.james@csiro.au System Administrator voice: 02 6246 5040 CSIRO Bioinformatics Facility fax: 02 6246 5166
participants (3)
-
Anders Johansson
-
Jay Vollmer
-
Michael James