How do I correctly return context from a gnuwin32 grep (grep in windows cmd)?

0

I want to return a line of context after the string I'm searching for as per grep --help

Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context

However,

  grep -a1 string1 file.txt-

gives me context before AND after (same with b1)

  grep -a 1 string1 file.txt

searches for "1" in the file "string1"

and, just to make things weirder,

  grep -c1 string1 file.txt

gives me nothing at all

thank you in advance to anyone who can help :)

Some_Guy

Posted 2015-05-09T20:26:56.623

Reputation: 684

2Are you using capitals for your -A1 or lower case as your question has? -a and -A are different flags after all, I would have thought lower case would always search for 1 in a file called string1 – Eric Renouf – 2015-05-09T20:30:54.867

fantastic. Sometimes it's the simple things :) – Some_Guy – 2015-05-09T23:06:05.783

1I wonder why a1 works as a substitute for C 1 at all though... – Some_Guy – 2015-05-09T23:09:07.787

Maybe 'cause Windows, unlike most *nixes, is case insensitive. I thought that was only a file system thing but apparently it isn't. – terdon – 2015-05-09T23:09:58.810

Answers

0

Please not that for GNU utilities (and in general), casing of the command line options used matter. Try

grep -C1 string1 file.txt

instead. Note the capital 'C'!

BTW, the '-c' flag means count. Instead of showing all individual occurrences it just shows the total number of lines the string occurs on in your file(s).

Atafar

Posted 2015-05-09T20:26:56.623

Reputation: 151