To find files having filename containing specific date format

Hi,

I have a requirement to create a shell script(tcsh) that finds all the files in a directory having the file name containing date format "YYYYMMDDHHMM" and extract the date time part ""YYYYMMDDHHMM" for further processing.

Could you please have any idea on this.

trades_201604040000.out
trades_201604040300.out
trades_201604040600.out

Thanks.

On requests like this you're much more likely to get a response with a shell that is based on the syntax used by the Bourne shell and expanded upon in the POSIX standards.

Why do you have to use tcsh for this?

What have you tried on your own?

Cumbersome in tcsh, you need an external command (sed)

#!/bin/tcsh
set head="trades_"
set tail=".out"
set nonomatch
foreach file ( ${head}*${tail} )
  if ( -f "$file" ) then 
    set datepart=`echo "$file" | sed 's/^'"$head"'//; s/'"$tail"'$//'`
    echo "$datepart"
  endif
end

Better in bash/ksh/psh:

#!/bin/bash
head="trades_"
tail=".out"
for file in ${head}*${tail}
do
  if [ -f "$file" ]
  then 
    datepart=${file#$head}
    datepart=${datepart%$tail}
    echo "$datepart"
  fi
done

Thanks for the reply.

Sorry for I am not being clear.

Please note that there may be files like below. I need to only find files having this pattern trades_yyyymmddhh24mm .

trades_201604040000.out
trades_201604040300.out
trades_201604040600.out
trades_20160406.out
trades_20160407.out

Or

for FN in tr*; do echo ${FN//[^0-9]}; done
201604040000
201604040300
201604040600

Use for FN in *_????????????.*

Replace * with ???????????? or more precisely with [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]