Re: writing cron job to print query results

From: John-Paul Stewart (jpstewart_at_sympatico.ca)
Date: 07/11/03


Date: Fri, 11 Jul 2003 14:38:52 -0400

Robin wrote:
>
> Bill Marcum <bmarcum@iglou.com> wrote in message news:<rrg1u-ldn.ln1@don.localnet>...
> > On 10 Jul 2003 16:18:35 -0700, Robin
> > <robin@witkopstaub.com> wrote:
> > > I want to search a text file for a condition and then if the condition
> > > exists print a line from above the condition. Sample of my text file
> > > is below
> > >
> > > user = sample {
> > > default service = permit
> > > login = cleartext "password"
> > > chap = cleartext "password"
> > > service = ppp protocol = ip {
> > > default attribute = permit
> > > }
> > > }
> > >
> > > I want to search for the login line, and if it exists print out the
> > > user = name from above. Is this possible to do with a grep and if
> > > statement in a cron job?
> >
> > grep -B 2 login < filename | head -1
> > assuming that "user = " is always two lines above "login".
>
> Thanks! The above command does work but I need it to search the entire
> file for any instance of the above condition. The listed command only
> shows my first user account found. The section above is duplicated
> about 100's of times and only a few have the login line in them. How
> do I make it start at the top of the file and search all the way
> through listing all users it finds with the login line?

grep always searches the entire file. The "-B 2" option is then
printing the two preceeding lines for every occurence of the pattern.
The problem is arising with the pipe to 'head -1', which is taking just
the first line of the entire output set. As an example try just:

grep -B 2 login < filename

It should print out every occurence of "login" plus the two lines
leading up to each. If you're only interested in the "user =" bit of
that output, grep for that. For example:

grep -B 2 login < filename | grep user

will extract the lines containing "user" from the output of my previous
example.



Relevant Pages

  • Re: simpletest vs phpunit vs ...
    ... How do I use grep command in Linux and Unix like operating systems? ...
    (comp.lang.php)
  • Re: MacTeX 2007 and documentation
    ... may be the most useful command available when doing searches at the ... will find every file on your system that has the text "memoir" on it, ... If locate generates 30 lines of text, where each line is a file, grep ... I need a Unix guru to chat to for half an hour or so to put me straight. ...
    (comp.text.tex)
  • Re: appending syslog output to file
    ... I cannot find a postrotate function. ... 'postrotate' is a "command" in the logrotate ... grep mystring syslog.0>>myfile ... grep $NBR numbers ...
    (uk.comp.os.linux)
  • grep-2.12 released [stable]
    ... Jim [on behalf of the grep maintainers] ... or run this command from a git-cloned grep directory: ... Use a .sig file to verify that the corresponding file (without the ... run a command like this: ...
    (gnu.announce)
  • Re: MacTeX 2007 and documentation
    ... means you need expertise in your host operating system as well. ... locate memoir | grep pdf ... I'm not someone who lives at the command line, ...
    (comp.text.tex)