1

Suppose my text file looks like this:

hello world1
foo bar2
hello world3
foo bar4
hello world5
foo bar 

If I run this command grep -A 4 'hello' draft.txt, I thought it print something like this:

hello world1     <<<<<<< This line matches
foo bar2         <<< These lines are the printed because of -A 4
hello world3     <<<
foo bar4         <<<
hello world5     <<< 
hello world3     <<<<<<< this line matches
foo bar4         <<< These lines are printed because of -A 4
hello world5     <<<
foo bar          <<<
 ... < so on > ....

But what it actually does is that it prints this (which might be a feature/bug).

hello world1
foo bar2
hello world3
foo bar4
hello world5
foo bar 

Is this how it is supposed to work? how do I get it to behave like what I thought it would ? (what switch/option do I use?)

0

Other lines with matches count as context. You can do something to make the matches show up to make the output easier to read.

grep  --color -A 4 'hello' draft.txt

enter image description here

Non-contiguous groups of matches and context lines will be split up with -- on a line by itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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