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.
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.
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