Please always mention your os details it will help us to guide you more, anyways my command in POST#2 works fine for me, I am using bash.
touch A8399_M1426
THD=1400
for FILE in *_M*
do
val=${FILE##*_M}
if [[ $val -gt $THD ]]
then
echo "Deleting file named " $FILE
fi
done
Deleting file named A8399_M1426
As you can see above I have created a test file and then checked the code and it worked fine for me, you could try following too.
for FILE in *_M*
do
val=`echo $FILE | awk '{sub(/.*_M/,X,$0);print}'`
if [[ $val -gt $THD ]]
then
echo "Deleting file named " $FILE
fi
done
Deleting file named A8399_M1426
on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .
Note that the code RavinderSingh13 provided had an "M" not a "W" in the substitution search pattern (as you requested). But, what that awk script does is to find the first occurrence of the longest string in the 1st field ( $1 ) on an input line ending with the two characters "_" and "W" and, if a match was found, replace that string with the contents of the awk variable X . Since X has not been initialized in this script, it defaults to an empty string. Then, whether or not a change was made, print prints the line.