Search code examples
bashshellgrepremote-server

Problem using grep inside a bash script that run remote on a server


I m using a script that run remote on server via ssh. Inside the script I'm using this line from below:

ls | grep -oP "\d{4} -\d{2}-\d{2}"

On my local machine that run Ubuntu the script work fine. But when I try to run it remote I got this

grep: invalid option -- 'P'
BusyBox v1.24.1 multi-call binary.
Usage: grep [-HhnlLoqvsriwFE] [-m N] [-A/B/C N] PATTERN/-e PATTERN/...-f file [FILE]...

The first thing I thought was an alias problem, i tryed

type grep

Output is: grep is /bin/grep I think this is ok.

What worries me is BusyBox (I do not know what it is) but i think this can be the problem ?


Solution

  • You may use [0-9] / [[:digit:]] instead of \d with POSIX BRE (no option) or ERE (-E option):

    grep -o "[0-9]\{4\} -[0-9]\{2\}-[0-9]\{2\}"
    grep -oE "[0-9]{4} -[0-9]{2}-[0-9]{2}"
    

    Note that in the first command you need to escape the braces since unescaped { and } match literal brace symbols in a POSIX BRE regex. When escaped, they mean range (interval, limiting) quantifiers. And in the second command, POSIX ERE is enabled with -E, and the behavior is reverse: when the braces are escaped they are literal chars, else they are quantifiers.