Select older file if multiple file exists with same name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select older file if multiple file exists with same name
# 1  
Old 01-20-2010
Select older file if multiple file exists with same name

Hi

I want to select older file if multiple files exist with same name.
eg:There are five files in UNIX folder
  1. Sample1_20091231
  2. Sample1_20100110
  3. Sample1_20100115
  4. Sample2_20100115
  5. Sample3_20100115
However in file list i.e FileName.DAT I have data like
  • Sample1
  • Sample2
  • Sample3
I am using FileName.DAT to read content of FileNames.

If multiple files exist as in this case Sample1 , I want to select older one , i.e. Sample1_20091231

So in this case I want to select Sample1_20091231,Sample2_20100115,Sample3_20100115 and neglect
Sample1_20100110,Sample1_20100115.

How to implement it ?

Any Suggestions.
# 2  
Old 01-20-2010
Code:
$ ls Sam* |sort |awk -F "_" '!a[$1]++ {print $0}'
Sample1_20091231
Sample2_20100115
Sample3_20100115

# 3  
Old 01-20-2010
Thanks DostSmilie

That one-liner worked gr8 for meSmilie

KUDOS !!!Smilie

UNIX FORUMS ROCK!!!SmilieSmilieSmilieSmilieSmilie

---------- Post updated at 02:52 PM ---------- Previous update was at 11:34 AM ----------

Hi

I am using this code.

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.


eg: Content in filename.DAT
  1. Sample1
  2. Sample2
  3. ABC1
  4. Sample3
Files in folder are :
  1. Sample1_20091231
  2. Sample1_20100120
  3. Sample2_20100120
  4. Sample3_20100120
Using the above code I am getting :
  1. Sample1_20091231
  2. Sample2_20100120
  3. Sample3_20100120
But there is no entry for ABC1.

Output required is
  1. Sample1_20091231
  2. Sample2_20100120
  3. ABC1
  4. Sample3_20100120
Help me with this !!!
# 4  
Old 01-20-2010
Maybe something like this?
Code:
#!/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

# 5  
Old 01-20-2010
Thanks MITRASmilie

UNIX FORUMS ROCKKKKSmilieSmilieSmilieSmilieSmilie
# 6  
Old 01-20-2010
Another alternative:

Code:
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*.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search if file exists for a file pattern stored in array

Hi experts, I have two arrays one has the file paths to be searched in , and the other has the files to be serached.For eg searchfile.dat will have abc303 xyz123 i have to search for files that could be abc303*.dat or for that matter any extension . abc303*.dat.gz The following code... (2 Replies)
Discussion started by: 100bees
2 Replies

2. UNIX for Dummies Questions & Answers

How to check if the same file exists multiple times?

Hi Team , Is there a way I can check to see if the same file say , test.dat exists multiple times in the directory path ? Please help. Thanks Megha (5 Replies)
Discussion started by: megha2525
5 Replies

3. Windows & DOS: Issues & Discussions

Script that, if file exists in Samba share, moves file to Unix server

I'm looking to do pretty much what the title says. I want a script that runs, it can run on Unix or Windows, doesn't matter, and searches a Samba shares for a .txt file. If the file exists, the script will move (or possibly copy) the file from the Samba share into a directory on our Unix... (3 Replies)
Discussion started by: twcostello
3 Replies

4. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

5. Shell Programming and Scripting

Newbie.. Find if a file exists and open, if not create the desired file..

Hey all, I'm brand new to script writing, I'm wanting to make a script that will ask for a file and then retrieve that file if it exists, and if it doesn't exist, create the file with the desired name, and I'm completely stuck.. so far.. #! bin/bash echo "Enter desired file" read "$file" if ... (5 Replies)
Discussion started by: Byrang
5 Replies

6. UNIX for Dummies Questions & Answers

Need help finding a file where a pattern exists and the file has a timestamp

So, I know how to do some of this stuff on an individual level, but I'm drawing a blank as to how to put it all together. I have a pattern that I'm looking for in a log file. The log file I know came in yesterday, so I want to limit the search to that day's listing of files. How would I do... (5 Replies)
Discussion started by: kontrol
5 Replies

7. Shell Programming and Scripting

check a file is exists in multiple tar file?

I have list of ‘tar' format files in a folder. I want to check a particular file is exists in the tar files. Currently I am using the below command to check the scenario. tar -tvf first.tar | grep ‘myfile.txt' The command able to searched a single ‘tar' file only. But I want to search... (7 Replies)
Discussion started by: k_manimuthu
7 Replies

8. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

9. Shell Programming and Scripting

Need to write a script in UNIX to find a file if another file exists

So I have a lot of Java applications on my servers all having their own folder from the applications subdirectory. Now, I need to do the following. Search all the applications subdirectories for message.jar. If the message.jar file exists, I need to search the application directory for... (1 Reply)
Discussion started by: mmdawg
1 Replies

10. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies
Login or Register to Ask a Question