3

I'm trying to remove "Packet number 624 doesn't match" from a response so the obvious thing to try is

cat somefile.txt | grep -v "Packet number \d+ doesn't match"

If I remove the -v, just for testing, then it returns nothing. So maybe the command line is doing something with the \d or + first. So I have tried various combinations such as \\d+ \\d\+ \\\\d+ \\\\d\+ [0-9]+ [0-9]\+. Bingo!! That last one worked. Can someone explain what is going on here? If this is getting modified by the command line why does echo "\d+" still return \d+?

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
MikeKulls
  • 873
  • 1
  • 10
  • 22
  • `\d` is extended grep so you need to use `grep -E` – Wolph Jun 17 '14 at 01:43
  • possible duplicate of [Is \d not supported by grep's basic expressions?](http://stackoverflow.com/questions/6901171/is-d-not-supported-by-greps-basic-expressions) – CDahn Jun 17 '14 at 01:44
  • @Wolph that doesn't work either. – CDahn Jun 17 '14 at 01:44
  • Thanks, that works and answers that question. The question still remains as to why `[0-9]+` didn't work – MikeKulls Jun 17 '14 at 01:51
  • Most probably you have to avoid cat command. Your command should be `grep -v "Packet number [0-9]\+ doesn't match" somefile.txt` – Avinash Raj Jun 17 '14 at 02:18
  • ^ Yes, swinging that cat around is unnecessary — `grep ... file` should work. – l'L'l Jun 17 '14 at 02:21
  • The cat is just for demonstrating the problem, the output actually comes from radsniff which we use to sniff radius traffic – MikeKulls Jun 17 '14 at 02:36

3 Answers3

5

By default, grep uses basic regular expression and \d is (PCRE) syntax. It is not supported so you'll need to use ( [0-9] ) or ( [[:digit:]] ) instead, or use grep with option -P

Why doesn't [0-9]+ work?

  • In BRE, meta-characters like + lose their meaning and need to be escaped.

You can fix this by using one of the following:

grep -v "Packet number [0-9]\+ doesn't match"

OR

grep -v "Packet number [[:digit:]]\+ doesn't match"
hwnd
  • 69,796
  • 4
  • 95
  • 132
  • Thanks everyone for the answers. This clears up something that has confused me for a long time which is why regex "sort of work" in some cases. I'd always assumed that non PCRE was basically the same with some subtle differences that I would likely never encounter. – MikeKulls Jun 17 '14 at 02:38
1

-v means inverse, any lines not matching pattern will be listed -E is the right parameter [Correction] -E is the right parameter WORKS IN Mac OS X -P is the right parameter, TESTED IN Ubuntu

try this

//in somefile.txt:
Packet number 100 doesn't match -E
Pck number 100 doesn't match -v

use cat somefile.txt | grep -v "Packet number \d+ doesn't match" both lines appears use cat somefile.txt | grep -E "Packet number \d+ doesn't match" in Mac OS X use cat somefile.txt | grep -P "Packet number \d+ doesn't match" in Linux only "Packet number 100 doesn't match -E" appears, which is what you want.

Joe Tse
  • 593
  • 6
  • 7
0

grep's re syntax didn't support notations like \d. There is a option to use Perl's syntax, try this,

cat somefile.txt | grep -v -P "Packet number \\d+ doesn't match"

Please note you need to escaspe the backslash.

fwu
  • 359
  • 2
  • 10
  • A better fix is to use single quotes around your regex. Also, lose the [useless use of `cat`.](/questions/11710552/useless-use-of-cat) – tripleee Mar 19 '19 at 18:55