Needed script for the version file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Needed script for the version file
# 1  
Old 12-17-2012
Needed script for the version file

I need some help with the logic and syntax for a shell script (ksh) that will search a directory and look for similar files and save only the 5 versions. The version number is in the file name.
However, the files are of varying name lengths and may have 1 or many files, with no limit to the number of files.
the input file
Code:
A_14122012.txt
A_15122012.txt
A_16122012.txt
A_17122012.txt

the output should contain the present and past one file

Code:
A_16122012.txt
A_17122012.txt

i used the below code please sugest the correct script

Code:
for file in `ls *.txt | sort -r | nawk -F"[2012|.]" '{if(_[$1]<2){print $0;_[$1]++}}' `
do
    echo $file
done

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 12-17-2012 at 10:19 AM..
# 2  
Old 12-17-2012
From you question, I presume that the file name uses the date in the format ddmmyyyy, and that makes it a little tricky. Better to use yyyymmdd if you can, so the most significant digits are first. If you are stuck with that, can you be sure that the files (once complete) are not edited?

If so, you could:-
Code:
ls -1rt *.txt|tail -5

If the file timestamps cannot be guaranteed, you could change the date format in the file name, then it is a much easier sort:-
Code:
ls -1 *.txt | sort -n +0.2

This format of sort may have been superseded by now, so check your manual pages for sort to be sure.



I hope that this helps,
Robin
Liverpool/Blackburn

---------- Post updated at 02:34 PM ---------- Previous update was at 02:32 PM ----------

Also see this thread:-
https://www.unix.com/shell-programmin...#post302745441
# 3  
Old 12-17-2012
Try:

Code:
ls -1 *.txt | sort -t_ -k1,1 -k2.5,2.8rn -k2.3,2.4rn -k2,2.2rn | nawk -F_ 'H[$1]++<2'

or, if your sort only supports the obsolescent key definitions:

Code:
ls -1 *.txt | sort -t_ +0 -1 +1.4 -1.7nr +1.2 -1.4nr +1 -1.2nr | nawk -F_ 'H[$1]++<2'


Last edited by Chubler_XL; 12-17-2012 at 10:15 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy a file from directroy/ prior version to the directory/ new version

How to copy a file from directroy/ prior version to the directory/ new version automatically. (4 Replies)
Discussion started by: roy1912
4 Replies

2. Shell Programming and Scripting

Needed script to FTP a File and generate a quality checksum file

hi all i want a script to FTP a file and should generate a quality checksum file means when I FTP a file from one server to another server it should generate a QC file which should contain timestamp,no.of records in that file Thanks in advance saikumar (3 Replies)
Discussion started by: hemanthsaikumar
3 Replies

3. Shell Programming and Scripting

Script for download file with version

Hi, Need Shell script to download the file with version or date from internet Filename will be as Eg:File-2.3.1.zip Filename will keep on changing for every 4months as File-2.3.2 or File 2.4 Thanks, Anil (4 Replies)
Discussion started by: Anil2312
4 Replies

4. Shell Programming and Scripting

KSH - help needed for creating a script to generate xml file from text file

Dear Members, I have a table in Oracle DB and one of its column name is INFO which has data in text format which we need to fetch in a script and create an xml file of a new table from the input. The contents of a single cell of INFO column is like: Area:app - aam Clean Up Criteria:... (0 Replies)
Discussion started by: Yoodit
0 Replies

5. Shell Programming and Scripting

shell or perl script needed for ldif file to text file conversion

This is the ldf file dn: sdcsmsisdn=1000000049,sdcsDatabase=subscriberCache,dc=example,dc=com objectClass: sdcsSubscriber objectClass: top postalCode: 29600 sdcsServiceLevel: 10 sdcsCustomerType: 14 givenName: Adelia sdcsBlackListAll: FALSE sdcsOwnerType: T-Mobile sn: Actionteam... (1 Reply)
Discussion started by: LinuxFriend
1 Replies

6. Shell Programming and Scripting

Help needed with script to verify the version of BIND

I have tried thought of using instfix -ivqc | grep BIND , but this did not return the result I was looking for; it seem to list out the the different patches that had been applied to BIND. I'm actually looking for overall version, like you'd get when checking the OS level for instance. (1 Reply)
Discussion started by: sport
1 Replies

7. Programming

Why a shared version is needed

Trying to make a library and get this error : *** Warning: This library needs some functionality provided by -lming32. *** I have the capability to make that library automatically link in when *** you link to this library. But I can only do this if you have a *** shared version of the... (1 Reply)
Discussion started by: hirosima
1 Replies
Login or Register to Ask a Question