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:

1 comment:

  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