---------- Post updated at 02:52 PM ---------- Previous update was at 11:34 AM ----------
Hi
I am using this code.
#!/bin/ksh
while read line
do
ls $line |sort |awk -F "_" '!a[$1]++ {print $0}'
done<filename.DAT>fileselected.DAT
However if $line has some value Say 'ABC*' and there no file exist as ABC* then it will prompt error, I want to save an entry for those files also by displaying $line.
#!/bin/ksh
while read line
do
ls $line* > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "$line"
else
ls $line* |sort |awk -F "_" '!a[$1]++ {print $0}'
fi
done < filename.DAT > fileselected.DAT
while read f; do
set "$f"*
[ -e "$1" ] && f=$1
echo "$f"
done < filename.DAT > fileselected.DAT
Read a line from filename.DAT and use it as the basis for a glob. If there is a match, the shell expands the glob in the set command and replaces it with an already sorted list, which given the format of the filenames will place the oldest in $1. If there is no match, $1 will contain the unexpanded glob. We then check to see if $1 exists to decide what to echo.
Note: Simply comparing "$f"* to "$1" would probably work in this case, given the constrained filename format, but in general it could fail since it would be possible for there to be a file whose name is identical to the pattern it matches: f* could expand to match a file whose name is f*.