Skip to main content
added 319 characters in body
Source Link
hidigoudi
  • 560
  • 1
  • 8

The operator =~ in Bash shell is equivalent to grep -E GNU command. Perl regex are not recognized with it. You need to do something like :

~$ [ $(echo "$filename" | grep -Po "$regex") ] && echo "it matches" || echo "does not match"
it matches

to have an equivalent.

About grep options used :

-o, --only-matching       show only the part of a line matching PATTERN
-P, --perl-regexp         PATTERN is a Perl regular expression

With your original form this looks like :

if [[ $(echo "$filename" | grep -Po "$regex") ]]; then echo "it matches"; else echo "does not match"; fi

This works too :

if [ $(echo "$filename" | grep -Po "$regex") ]; then echo "it matches"; else echo "does not match"; fi

You have also the possibility to do :

yyy@xxx:~$ filename="test-33.csv"
yyy@xxx:~$ regex="([^.]+)(-\d{1,5})(\.csv)"
yyy@xxx:~$ result=$(echo "$filename" | grep -Po "$regex")
yyy@xxx:~$ if [[ $result ]]; then echo "it matches"; else echo "does not match"; fi
it matches
yyy@xxx:~$

The operator =~ in Bash shell is equivalent to grep -E GNU command. Perl regex are not recognized with it. You need to do something like :

~$ [ $(echo "$filename" | grep -Po "$regex") ] && echo "it matches" || echo "does not match"
it matches

to have an equivalent.

About grep options used :

-o, --only-matching       show only the part of a line matching PATTERN
-P, --perl-regexp         PATTERN is a Perl regular expression

With your original form :

if [[ $(echo "$filename" | grep -Po "$regex") ]]; then echo "it matches"; else echo "does not match"; fi

This works too :

if [ $(echo "$filename" | grep -Po "$regex") ]; then echo "it matches"; else echo "does not match"; fi

The operator =~ in Bash shell is equivalent to grep -E GNU command. Perl regex are not recognized with it. You need to do something like :

~$ [ $(echo "$filename" | grep -Po "$regex") ] && echo "it matches" || echo "does not match"
it matches

to have an equivalent.

About grep options used :

-o, --only-matching       show only the part of a line matching PATTERN
-P, --perl-regexp         PATTERN is a Perl regular expression

With your original form this looks like :

if [[ $(echo "$filename" | grep -Po "$regex") ]]; then echo "it matches"; else echo "does not match"; fi

This works too :

if [ $(echo "$filename" | grep -Po "$regex") ]; then echo "it matches"; else echo "does not match"; fi

You have also the possibility to do :

yyy@xxx:~$ filename="test-33.csv"
yyy@xxx:~$ regex="([^.]+)(-\d{1,5})(\.csv)"
yyy@xxx:~$ result=$(echo "$filename" | grep -Po "$regex")
yyy@xxx:~$ if [[ $result ]]; then echo "it matches"; else echo "does not match"; fi
it matches
yyy@xxx:~$
added 130 characters in body
Source Link
hidigoudi
  • 560
  • 1
  • 8

The operator =~ in Bash shell is equivalent to grep -E GNU command. Perl regex are not recognized with it. You need to do something like :

~$ [ $(echo "$filename" | grep -Po "$regex") ] && echo "it matches" || echo "does not match"
it matches

to have an equivalent.

About grep options used :

-o, --only-matching       show only the part of a line matching PATTERN
-P, --perl-regexp         PATTERN is a Perl regular expression

With your original form :

if [[ $(echo "$filename" | grep -Po "$regex") ]]; then echo "it matches"; else echo "does not match"; fi

This works too :

if [ $(echo "$filename" | grep -Po "$regex") ]; then echo "it matches"; else echo "does not match"; fi

The operator =~ in Bash shell is equivalent to grep -E GNU command. Perl regex are not recognized with it. You need to do something like :

~$ [ $(echo "$filename" | grep -Po "$regex") ] && echo "it matches" || echo "does not match"
it matches

to have an equivalent.

About grep options used :

-o, --only-matching       show only the part of a line matching PATTERN
-P, --perl-regexp         PATTERN is a Perl regular expression

With your original form :

if [[ $(echo "$filename" | grep -Po "$regex") ]]; then echo "it matches"; else echo "does not match"; fi

The operator =~ in Bash shell is equivalent to grep -E GNU command. Perl regex are not recognized with it. You need to do something like :

~$ [ $(echo "$filename" | grep -Po "$regex") ] && echo "it matches" || echo "does not match"
it matches

to have an equivalent.

About grep options used :

-o, --only-matching       show only the part of a line matching PATTERN
-P, --perl-regexp         PATTERN is a Perl regular expression

With your original form :

if [[ $(echo "$filename" | grep -Po "$regex") ]]; then echo "it matches"; else echo "does not match"; fi

This works too :

if [ $(echo "$filename" | grep -Po "$regex") ]; then echo "it matches"; else echo "does not match"; fi
added 141 characters in body
Source Link
hidigoudi
  • 560
  • 1
  • 8

The operator =~ in Bash shell is equivalent to grep -E GNU command. Perl regex are not recognized with it. You need to do something like :

~$ [ $(echo "$filename" | grep -Po "$regex") ] && echo "it matches" || echo "does not match"
it matches

to have an equivalent.

About grep options used :

-o, --only-matching       show only the part of a line matching PATTERN
-P, --perl-regexp         PATTERN is a Perl regular expression

With your original form :

if [[ $(echo "$filename" | grep -Po "$regex") ]]; then echo "it matches"; else echo "does not match"; fi

The operator =~ in Bash shell is equivalent to grep -E GNU command. Perl regex are not recognized with it. You need to do something like :

~$ [ $(echo "$filename" | grep -Po "$regex") ] && echo "it matches" || echo "does not match"
it matches

to have an equivalent.

About grep options used :

-o, --only-matching       show only the part of a line matching PATTERN
-P, --perl-regexp         PATTERN is a Perl regular expression

The operator =~ in Bash shell is equivalent to grep -E GNU command. Perl regex are not recognized with it. You need to do something like :

~$ [ $(echo "$filename" | grep -Po "$regex") ] && echo "it matches" || echo "does not match"
it matches

to have an equivalent.

About grep options used :

-o, --only-matching       show only the part of a line matching PATTERN
-P, --perl-regexp         PATTERN is a Perl regular expression

With your original form :

if [[ $(echo "$filename" | grep -Po "$regex") ]]; then echo "it matches"; else echo "does not match"; fi
added 1 character in body
Source Link
hidigoudi
  • 560
  • 1
  • 8
Loading
Source Link
hidigoudi
  • 560
  • 1
  • 8
Loading