script to find filenames with latest version and for all seq. numbers in a day

Hi,

We have a requirement to find the set of filenames from the group of files in a specified folder based on
(i) version number
(ii) sequence number
such that, for any given sequence number in a day only the latest version filenames have to indentified.
Below is the format of the file

<HUB>_<FEED_NAME>_<DATE>_<SEQUENCE_NUM>_<VERSION_NUM>.pgp

Hence if the folder has files namely

  • dummy_feed_02022010_1_1.pgp
  • dummy_feed_02022010_1_2.pgp
  • dummy_feed_02022010_1_3.pgp
  • dummy_feed_02022010_2_2.pgp
    -dummy_feed_01022010_1_1.pgp

In the above case, if the shell script is run on 2 nd feb assuming, it should pick files for that day with all sequence numbers but only the latest versions.

here, the shell script should return

dummy_feed_02022010_1_3.pgp
dummy_feed_02022010_2_2.pgp

Please help me out how to get this done using a shell script. Let me know if you need more details.

Thanks
Deepak

Try this:

awk -F"_" -v d=$(date "+%d%m%Y") '
$3==d && $5>n[$3$4] {n[$3$4]=$5;a[$3$4]=$0}
END{for(i in a){print a}}' file | sort

Use nawk or /usr/xpg4/bin/awk on Solaris.

#!/usr/bin/ksh
pseq=""
pver=""
phub=""
pfed=""
pdat=""
cat a.txt | grep `date  '+_%d%m%Y_'` | sort -t'_' +2 +3 +4 > out.txt
while IFS="_" read hub feed tdate seq ver ; do
  if [[ "$seq" != "$pseq" ]]
  then
     if [[ "$pseq" == "" ]]
     then
        pseq=$seq
        pver=$ver
        phub=$hub
        pdat=$tdate
        pfed=$feed
     else
        echo "${hub}_${feed}_${tdate}_${pseq}_${pver}"
        pseq=$seq
        pver=$ver
        phub=$hub
        pdat=$tdate
        pfed=$feed
     fi
  else
    if [[ "$pver" < "$ver" ]]
    then
      pver=$ver
    fi
  fi
done < out.txt > out
echo "${phub}_${pfed}_${pdat}_${pseq}_${pver}" >> out
rm -f out.txt

Or...

#!/bin/ksh
d=`date +"%d%m%Y"`
sort -t"_" -r -k345 infile | while read line
do
v1=`echo $line | cut -d"_" -f1`
v2=`echo $line | cut -d"_" -f2`
v3=`echo $line | cut -d"_" -f3`
v4=`echo $line | cut -d"_" -f4`
v5=`echo $line | cut -d"_" -f5 | cut -d"." -f1`
if [[ $d -eq $v3 ]];
then
 if [[ $v4 -ne $prv4 ]];
 then
 print $v1"_"$v2"_"$v3"_"$v4"_"$v5".pgp"
 fi
prv4=$v4
fi
done

Thanks everyone. With slight modification i am able to run the script. Thanks much.

Regards
Deepak