Help!! how to get the filename as be shown?

I am trying to obtain the file name (not including sub file name), however, I still cannot have the string output. :frowning:

2 existed files at /tmp, AAA.new and BBB.last
Originally, the output result is needed to be shown as follows,
ex:
SYSTEM NEW LAST

01 AAA BBB

My script was as follows, please kindly review and modify!!

if [ ! ls -t -a $filename/.new || -a $filename/.last ]; then
echo "------------NA-------------"

elif [-a $filename\*.new]; then
echo "$SYSTEM\ $filename" | read filename

elif [-a $filename\*.last]; then
echo "$SYSTEM\ $filename" | read filename

fi

There are several errors and one or two other changes. Try:

if [[ !  -a $filename/*.new || ! -a $filename/*.last ]]; then
    echo "------------NA-------------"
else
   if [[-a $filename/*.new]]; then
      filename="$SYSTEM/$filename" 
   else
      filename="$SYSTEM/$filename" 
   fi
fi

If I get it correctly, you just want the filenames without the extensions.

for i in *; do
ext=`echo $i | awk -F"." '{print "."$2}'`;
filename=`basename $i $ext`;
echo $filename;
done

Thanks for your kindly support, but I still cannot run smoothly,
I consider the loop has a issue, the result was as attached,
SYSTEM NEW LAST

pp01 ==========>NA
pp02 ==========>NA
pp03 ==========>NA
pp04 ==========>NA
.........

My script was as follows,

echo "SYSTEM NEW LAST"
echo "======================"
for test_name in $(grep -e "pp" $test_list | awk '{ print $2 }')
do
echo "$test_name\c"
[ $( ls -1 $pc_dir | grep -c $test_name ) -eq 0 ] && echo "$test_name ===========> N/A"
continue

for filename in $\( ls -1 $pc_dir \) 
do
  ext=$\(echo $filename | awk -F. '\{print $2\}'\)
  pc_name=$\(echo $filename | awk -F _ '\{print $2 "_" $3\}'\)
 
if [ "$ext" = "new" ] 
then	
    echo "$test_name	$\{pc_name%.new\}"

elif [ "$ext" = "last" ]
then	
    echo "$test_name        	$\{pc_name%.last\}" 
fi
    done   

done

Try this (not tested)

echo "SYSTEM NEW LAST"
echo "======================"

for test_name in $(awk '/pp/ {print $2}')
do
   if ls -1 | grep -q $test_name
   then
      ls -1 $pc_dir |
      while read filename
      do
         pc_name=$(echo $filename | cut -d_ -f1-2)
         ext=$(echo $filename | awk -F. '{print $2}')
         case "$ext" in
            new)  echo $test_name ${pc_name%.new}"  ;;
            last) echo $test_name ${pc_name%.last}" ;;
         esac
      done
   else
      echo "$test_name ===========> N/A"
   fi
done

Jean-Pierre.

Could you provide some sample file names, if it still doesn't works.

Dear Sirs (Above!),

Thanks for your kindly help!!
I have fixed it and also appreciated your support.
:slight_smile:

Thanks a lot as your support and help

I have tried you and aigles' example and those are useful to me. :slight_smile:
Thanks a lot!!