All Articles

The ABC's of grep - P

P

Use the -P flag for Perl compatible regular expressions.

Let’s recall our test.txt file with the following content:

sad
happy
awake
coffee
work
school

Let’s add a line with digits.

$ echo "3.14" >> test.txt
$ cat test.txt
sad
happy
awake
coffee
work
school
3.14

In Perl, \d is a regular expression that matches on numerical digits.

Let’s try executing a grep command with it.

$ grep "\d" test.txt
sad

As you can see, there is a match on the literal d as oppose to matching on digits.

Running the command with the -P flag should do the trick:

$ grep -P "\d" test.txt
3.14

Published Nov 13, 2023

I love coffee, coding and writing.