Pattern match

Hi all,

i am using this code , but having some problem,

pattern=File_[0-9]*.csv
pattern1=File_[0-9]*_[0-9]*.csv

and then i am comparing the patterns like this

case ${arr[$i]} in
  $pattern) echo "pattern %N% match" ;;
  esac
case ${arr[$i]} in
  $pattern1) echo "pattern %N%_%N%  match" ;;
  esac

but if name of my file is File_23_23.csv
then it is returning

pattern %N% match
pattern %N%_%N%  match

but the desired result should be only pattern %N%_%N% match only.

can any one please help me.

You've got a couple of problems. First, you define pattern and pattern1, but you never use pattern1. And, second, these are filename matching patterns, not regular expressions. So the filename matching pattern File_[0-9]*.csv will match any string that starts with "File_" followed by a single digit followed by any number of any character, followed by ".csv" at the end of the string.

If you want to match a filename like File_23_23.csv you could use File_[0-9][0-9]_[0-9][0-9].csv .

but i dont know the length of that i.e it can be

File_23_23.csv
or
File_112_156.csv
or
File_11111_55577.csv

or it can be any number.

and other thing
File_12_45.csv is being matched by File_[0-9].csv i.e underscore is also being taken in [0-9] although it is not a numeral.
is there any way i can distinguish between these types of file like File_%N%.csv and File_%N%_%N%.csv

Thanks

What shell are you using?
What OS are you using?

shell is bash
os - linux

The following seems to do what you want and works with both recent versions of bash and Korn shells with a version of 1993 or later:

#!/bin/bash
arr=("File_23_23.csv" "File_112_156.csv" "File_11111_55577.csv" "File_23.csv" "File_112.csv" "File_11111.csv" "File_xyz.csv")
for i in "${arr[@]}"
do      printf "Processing %s:\n" "$i"
        if [[ "$i" =~ File_[0-9]+.csv ]]
        then    echo 'Single digit string.'
        elif [[ "$i" =~ File_[0-9]+_[0-9]+.csv ]]
        then    echo 'Double digit string.'
        else    echo 'No match'
        fi
done

producing the output:

Processing File_23_23.csv:
Double digit string.
Processing File_112_156.csv:
Double digit string.
Processing File_11111_55577.csv:
Double digit string.
Processing File_23.csv:
Single digit string.
Processing File_112.csv:
Single digit string.
Processing File_11111.csv:
Single digit string.
Processing File_xyz.csv:
No match