Grep with surrounding lines

|

Yesterday I wanted to find a maven dependency in my project, that itself depended on another dependency, which had a security issue and needed updating.

The command

mvn dependency:tree

displays the whole dependency tree, but in a large project it takes a while to find what you're looking for. So I grepped the result

mvn dependency:tree | grep "<name>"

which confirmed the dependency was present, but not in which context. Fortunately there are the following switches

  • -A <number> : number of lines to be displayed after the match
  • -B <number> : number of lines to be displayed before the match
  • -C <number> : number of lines to be displayed before and after the match

So I was able to find the dependency in my project with

mvn dependency:tree | grep "<name>" -B 10

If there is a better way to do this, let me know :)

blog comments powered by Disqus