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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to find filenames with latest version and for all seq. numbers in a day
# 1  
Old 02-08-2010
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

Last edited by pludi; 02-08-2010 at 03:07 AM.. Reason: sanitized formatting
# 2  
Old 02-08-2010
Try this:

Code:
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[i]}}' file | sort

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 3  
Old 02-08-2010
Try this.

Code:
#!/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


Last edited by Karpak; 02-08-2010 at 05:15 AM..
# 4  
Old 02-08-2010
Or...

Code:
#!/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

# 5  
Old 02-08-2010
script to find filenames with latest version and for all seq. numbers in a day

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

Regards
Deepak
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to find seq lines

I have a below file FILE.cfg JAN_01 VAR1=4 VAR2=SUM VAR3=PRIVATE JAN_10 VAR1=44 VAR2=GUN VAR3=NATUR JAN_20 VAR1=3 VAR2=TQN VAR3=COMMA code: (JAN_10 is argument passed from script) (6 Replies)
Discussion started by: Roozo
6 Replies

2. Shell Programming and Scripting

How to find the date of previous day in shell script?

Hi Experts, i am using the below code get the date of previous day. #!/usr/bin/ksh datestamp=`date '+%Y%m%d'` yest=$((datestamp -1)) echo $yest When i execute the code i am getting output as: 20130715 What i am trying here is, based on the date passed i am fetching previus day's... (0 Replies)
Discussion started by: learner24
0 Replies

3. Shell Programming and Scripting

Converting filenames from julian day to yyyy-mm-dd and retrieving weekly mean values

Hi, I need help to convert the filenames of my 9-year daily files (1999-2007) from a julian day to yyyy-mm-dd format. my original files are patterned likes the ones below. 1999001.txt 1999002.txt 1999003.txt 1999004.txt ... 1999365.txt desired output: 19990101.txt 19990102.txt... (3 Replies)
Discussion started by: ida1215
3 Replies

4. Fedora

Script to find out first day of our calender

I try to find the first day of our calender. So I used this script ... echo -n "The week of the date 01jan0001 : " echo -n `date -d 00010101 +%A` echo But its shows error bash-3.1$ sh first_day.shThe week of the date 01jan0001 : date: invalid date `00010101' (3 Replies)
Discussion started by: krishnampkkm
3 Replies

5. Shell Programming and Scripting

Find out the day in Bash Shell script

Hello All, I need a bash shell script to find out a day from the date.For example we give the date(20100227/YYYYMMDD) then we get the day 'Saturday'. Thanks in advance, Satheesh (5 Replies)
Discussion started by: satheesh4093
5 Replies

6. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

7. Shell Programming and Scripting

shell script to find latest date and time of the files

Hi everyone, Please help:) I have a list of 1000 different files which comes daily to the directory.Some of the files are not coming to the directory now. I need to write a shell script to find the latest date and time of the files they came to the directory. The files should be unique.... (1 Reply)
Discussion started by: karthicss
1 Replies

8. Shell Programming and Scripting

script to find latest executable in particular directory and start that particular ex

i have a directory in which there are executable files and these files are added at runtime. now i need a shell script which will be called at a certain interval. this shell script should find the latest executable file in that directory and start that executable. care should be taken that once the... (6 Replies)
Discussion started by: kvineeth
6 Replies

9. Shell Programming and Scripting

Write a shell script to find whether the first day of the month is a working day

Hi , I am relatively new to unix... Can u pls help me out to find out if the first day of the month is a working day ie from (Monday to Friday)...using Date and If clause in Korn shell.. This is very urgent. Thanks for ur help... (7 Replies)
Discussion started by: phani
7 Replies

10. UNIX for Dummies Questions & Answers

What is the latest version of Unix?

I want to buy it (2 Replies)
Discussion started by: LANSTARR.COM
2 Replies
Login or Register to Ask a Question