JP Vossen via plug on 10 Feb 2021 15:03:24 -0800


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Scripting question


Yup, what Carl said.  See also -B (before) and -C (context; before and after).  Or -n add line number to output, -o to show only what matched (not the whole line), -q to be quiet and only set an exit code, or -c to just count the matches, if any.

`grep -c 'foo' /var/log/* | grep -v ':0$'` is quite handy to find log files containing 'foo'.  The second `grep` omits any lines (files) ending in a zero count.

`man grep` is useful, but there are a lot of options to digest and it's not the best introduction to regular expressions.  Friedl's _Mastering Regular Expressions_ is THE BOOK, but it's dense, bring aspirin.  It's great, don't get me wrong.  It's just...dense.

On 2/10/21 5:48 PM, Carl Johnson via plug wrote:
Add the "-An" argument. Where the "n" is the number of lines that you want to see after the match.

On Wed, Feb 10, 2021, 5:45 PM Michael Lazin via plug <plug@lists.phillylinux.org <mailto:plug@lists.phillylinux.org>> wrote:

    I like to think I am good at bash scripting but I am a little bit stumped.  I am used to using commands like grep and egrep, but I want to match a pattern in text and then output not just the line that has the pattern but also a few subsequent lines.  Can someone please give me a suggestion?  Thanks again for your help
Later,
JP
--  -------------------------------------------------------------------
JP Vossen, CISSP | http://www.jpsdomain.org/ | http://bashcookbook.com/
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug