there are mutiple file nams in the directory. How to return the the lastest files for each file name.
ex.
abc1234_050201
abc1234_050206
abc1234_050208
xyz34_050204
xyz34_050210
xyz34_050218
thanks
there are mutiple file nams in the directory. How to return the the lastest files for each file name.
ex.
abc1234_050201
abc1234_050206
abc1234_050208
xyz34_050204
xyz34_050210
xyz34_050218
thanks
Try:
ls | sort -t_ -k2,2rn | awk -F_ '!A[$1]++'
the script still rturns lots file name but not the one for each name pattern. thanks
This works for me:
tmp1 $ ls
34567_a 34567_b 34567_c 34567_d abc_a abc_b abc_c abc_d ascript.sh
+ tmp1 $ bash ascript.sh
34567_d
abc_d
+ tmp1 $ cat ascript.sh
newlist=""
for f in *_*;do
nl=${f/_*/}
echo "$newlist" | grep -q "$nl" || newlist+=" $nl"
done
for nl in $(echo $newlist|sort -u);do
for this in $nl*;do echo > /dev/zero;done
echo $this
done
hth
With your sample file names I get:
ls | sort -t_ -k2,2rn | awk -F_ '!A[$1]++'
xyz34_050218
abc1234_050208
What do you get?
Otherwise try:
ls xyz*_* abc*_* | sort -t_ -k2,2rn | awk -F_ '!A[$1]++'
xyz34_050218
abc1234_050208
or
ls xyz*_* abc*_* | sort -t_ -k2,2rn | awk -F_ '!A[$1]++'
\ls xyz*_* abc*_* | sort -t_ -k2,2rn | awk -F_ '!A[$1]++'