I need help to compare file name with regular expression and do something with file

#!/bin/sh
Today=date '+%Y%m%d' 
for file in `ls *.csv *txt` 
do          
        echo "Start woprking with ${file}"
         if [ "${file}" = "*_${Today}*" ];  then
                 do something
         elif [ "${file}" = "*.${Today*}" ];  then
               do something 
               else
                   echo "Unkniowned file name"
               fi
        fi
done

Files names like this:

asd_ajfh_kjghjkg_20170227.csv
asd_ajfh_kjghjkg.20170227.csv
iytouy_jkhk_jghfadj_123_6453_20170227_U.csv 

Script print first line before first if and doing nothing. Can somebody let me know where I am wrong? I am on AIX, korn, born, or c

I need it because depends of _Today .Today files will be processed differently

Why not only select the files that match your required pattern when listing the files in the first place? This might get you started:-

#!/bin/ksh
Today=`date '+%Y%m%d'`
for file in *[._]${Today}*
do
   something with "${file}"
done

Note that I've added ` around the date command. Although I think this is deprecated, I'm not sure if your shell will support the $(date '+%Y%m%d') form.

Are there any files with spaces in the name that we have to handle?

Does this help?
Robin

I have 70 different files. Some of them are: XXX_Today.csv , some of them XXXX.Today.csv . All of them can have different amount of XXXXX (fro 2 to 5) divide by _. The main problem is that I have to find the file, find Today and cut it from the main part, and what has left, for example XXX_CCCC_BBBB I open file find this part and make short form, like XCB and son on. I will check about Today , may be you are right. I will let you know as soon as I get to work

How would iytouy_jkhk_jghfadj_123_6453_20170227_U.csv fit into that scheme?

Do you have another solution?

---------- Post updated at 09:26 AM ---------- Previous update was at 08:53 AM ----------

Yes, my shell handle this format and before catch file I print ${Today} and it show me 20170228

---------- Post updated at 09:35 AM ---------- Previous update was at 09:26 AM ----------

Robin

Note that I've added ` around the date command. Although I think this is deprecated, I'm not sure if your shell will support the $(date '+%Y%m%d') form.

Are there any files with spaces in the name that we have to handle?

Yes, my shell handle this format and before catch file I print ${Today} and it show me 20170228

Oleg

Guessing from your original code that you only want to process files with names ending with .csv or .txt , I would tend to try something like:

#!/bin/ksh
Today=$(date '%Y%m%d')
for file in *$Today*.csv *$Today*.txt
do	if [ -f "$file" ]
	then	echo do whatever with "$file"
	fi
done

or if you know that you won't encounter filename extensions like cxt and cst , you could shorten:

for file in *$Today*.csv *$Today*.txt

to:

for file in *$Today*.[ct][sx][vt]

No,
I need files *${Today}*.csv *${Today}.txt
and *.${Today}
.csv *.${Today}*.txt

For the second line I made a loop and it is perfectly fine.
The first line is still there are still a lot of questions, because I don't know the precise files names. When I find out, I will ask for help