Wednesday, July 5, 2017

Grep extract before and after a line

If you need to extract lines before and after a line with a string you search for using grep use -B flag for before and -A for after to get lines wanted:

grep -A3 -B2 1998 libtool 
# NOTE: Changes made to this file will be lost: look at ltmain.sh.
#
# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
# Free Software Foundation, Inc.
#
# This file is part of GNU Libtool:
If you need same number of lines before and after use -C flag

grep -C3 1998 libtool 
# Generated automatically by  (GNU  )
# NOTE: Changes made to this file will be lost: look at ltmain.sh.
#
# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
# Free Software Foundation, Inc.
#
# This file is part of GNU Libtool:

3 comments:

  1. this blog is for those who always find themselves sworming in the world of codes. i had friend who always had her nose in her computer screen forming codes i can only wonder what would she have to say about these ones!

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. thanks for the tips and information..i really appreciate it.. fabp3 elisa

    ReplyDelete