1

This question already has an answer here:

I use,

egrep -rnw . -e "string"

a lot (in bash), but it would be nice to add some context to the returned results. This would be the line before and after it. Maybe the two lines before and afer it.

Is this difficult to do?

grep, egrep, pcregrep, etc. does not matter.

Thanks

PS. I just saw this: Search for a string in a line and append related lines which I didn't think of searching for "append".

marked as duplicate by don_crissti, Community Sep 22 '17 at 16:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Have you looked at the -A and -B options? – garethTheRed Sep 22 '17 at 16:10
  • I saw that but I was thinking those lines still had to have "string" in them. I guess I was misunderstanding. – nate Sep 22 '17 at 16:12
  • Or -Cn, which is the same as -An -Bn for n lines. The mnemonic I use is After/Before/Context. – DopeGhoti Sep 22 '17 at 16:28
1

Many grep implementations have -A n, -B n and -C n (where -C n is equivalent to -A n -B n).

The n above is the number of lines of context that you would like to have for each matching line. -A would give you context above the match, -B below, and -C around on both sides.

These options are extensions to the standard description of the grep utility.

Not the answer you're looking for? Browse other questions tagged or ask your own question.