getting diffrent results from a hackerrank bash practice than my acutal bash

Viewed 82

I have been doing some hackerrank practices, mainly about the grep shell command. The challenge asks me to detect a pattern on bank cards number, which is to detect pair of numbers next to each other, or with a space in between. Here is an example of the pattern :

5678 9101 1234

2[9][9][9] 5178 9101 234

[9][9][9][9] 5628 9201 1232

I tried this ERE regex which gives the appropriate output on my bash :

>  grep -E '([0-9])([[:space:]]*)\1'

PS : Hackerrank does automatically pipe the input file to the grep command

my attempt on Gnu/Linux bash

However, i get false output, or i strictly saying (different output than my OS bash)

Hackerrank's bash

Here's the challenge link : https://www.hackerrank.com/challenges/text-processing-in-linux-the-grep-command-5/problem

my question is:

  • does the bash shell on Hackerrank works differently than my OS bash shell ? if YES, then what are the differences and is there any resources that i can rely on. In case the answer is NO, then why the outputs differ ?
1 Answers

For some reason, Hackerrank requires the use of POSIX BRE here.

You can use

grep '\([0-9]\)[[:space:]]*\1'
grep '\([0-9]\) *\1'
grep '\([0-9]\)\s*\1'

The \(...\) is used to define a capturing group in POSIX BRE regex.

Related