Selecting one file from a list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selecting one file from a list
# 1  
Old 09-23-2008
Selecting one file from a list

Hi,

I am able to do this by brute force but I am just curious if there is a better way of handling things. Basically the scenario is something like this:

There are a number of files in a directory:
Code:
[   ] rib.20071224.1759.gz    24-Dec-2007 17:59  132K  
[   ] rib.20071224.1959.gz    24-Dec-2007 19:59  132K  
[   ] rib.20071224.2159.gz    24-Dec-2007 21:59  132K  
[   ] rib.20071224.2359.gz    24-Dec-2007 23:59  132K  
[   ] rib.20071225.0159.gz    25-Dec-2007 01:59  132K  
[   ] rib.20071225.0359.gz    25-Dec-2007 03:59  132K  
[   ] rib.20071225.0559.gz    25-Dec-2007 05:59  132K  
[   ] rib.20071225.0759.gz    25-Dec-2007 07:59  132K  
[   ] rib.20071225.0959.gz    25-Dec-2007 09:59  132K  
[   ] rib.20071225.1159.gz    25-Dec-2007 11:59  132K  
[   ] rib.20071225.1359.gz    25-Dec-2007 13:59  132K  
[   ] rib.20071225.1559.gz    25-Dec-2007 15:59  132K  
[   ] rib.20071225.1759.gz    25-Dec-2007 17:59  132K  
[   ] rib.20071225.1959.gz    25-Dec-2007 19:59  132K  
[   ] rib.20071225.2159.gz    25-Dec-2007 21:59  132K  
[   ] rib.20071225.2359.gz    25-Dec-2007 23:59  132K  
[   ] rib.20071226.0159.gz    26-Dec-2007 01:59  132K  
[   ] rib.20071226.0359.gz    26-Dec-2007 03:59  132K  
[   ] rib.20071226.0559.gz    26-Dec-2007 05:59  132K  
[   ] rib.20071226.0759.gz    26-Dec-2007 07:59  132K  
[   ] rib.20071226.0959.gz    26-Dec-2007 09:59  133K  
[   ] rib.20071226.1159.gz    26-Dec-2007 11:59  132K  
[   ] rib.20071226.1359.gz    26-Dec-2007 13:59  133K  
[   ] rib.20071226.1559.gz    26-Dec-2007 15:59  132K  
[   ] rib.20071226.1759.gz    26-Dec-2007 17:59  133K  
[   ] rib.20071226.1959.gz    26-Dec-2007 19:59  133K  
[   ] rib.20071226.2159.gz    26-Dec-2007 21:59  133K  
[   ] rib.20071226.2359.gz    26-Dec-2007 23:59  132K  
[   ] rib.20071227.0159.gz    27-Dec-2007 01:59  132K  
[   ] rib.20071227.0359.gz    27-Dec-2007 03:59  132K  
[   ] rib.20071227.0559.gz    27-Dec-2007 05:59  132K  
[   ] rib.20071227.0759.gz    27-Dec-2007 07:59  132K  
[   ] rib.20071227.0959.gz    27-Dec-2007 09:59  133K  
[   ] rib.20071227.1159.gz    27-Dec-2007 11:59  131K  
[   ] rib.20071227.1359.gz    27-Dec-2007 13:59  132K

I want to be able to copy only one file from each group i.e. basically when I have the following:

Code:
[   ] rib.20071224.1759.gz    24-Dec-2007 17:59  132K  
[   ] rib.20071224.1959.gz    24-Dec-2007 19:59  132K  
[   ] rib.20071224.2159.gz    24-Dec-2007 21:59  132K  
[   ] rib.20071224.2359.gz    24-Dec-2007 23:59  132K

I want to select only the first file or one of the files but I want just one. So in this case, I would like to copy only rib.20071224.1759.gz. Is there a good way to solve this problem? As of now, I am using a scripting language like php to achieve this. Any advice is greatly appreciated.

Thanks
# 2  
Old 09-23-2008
Code:
ls rib* | awk -F. '!a[$2]++' | xargs -i cp -p  '{}' /path/to/other_dir

# 3  
Old 09-23-2008
Thanks a lot... Could you be kind enough to explain what exactly that is doing? I'm new to awk so any input is appreciated... From what I am assuming, it is basically storing names as keys and then taking care that there are no duplicate keys... is it? But still it seems a little surprising to me that it works...
# 4  
Old 09-24-2008
Quote:
Originally Posted by Legend986
Thanks a lot... Could you be kind enough to explain what exactly that is doing? I'm new to awk so any input is appreciated... From what I am assuming, it is basically storing names as keys and then taking care that there are no duplicate keys... is it? But still it seems a little surprising to me that it works...
Code:
awk -F. '!a[$2]++'

-F. - set field separator to a dot.
a[$2]++ - It's simply a counter, which is raised every time the same record is seen, and the field 2 of the filename is the key of the array a.
Code:
![a$2]++

It's the same as writing:

Code:
 { a[$2]=a[$2]+1
               if ( a[$2]==1 ) print $0 }

or

Code:
 a[$2]++==0 { print $0 }

This condition is true only when the record is seen the first time.
By the same token if you need the second occurrence of the same filename, then the condition would have been:
Code:
a[$2]++==1 { print $0 }

for third :
Code:
a[$2]++==2 { print $0 }

... and so on

Code:
xargs -i cp -p  '{}' /path/to/other_dir

On the list of files filtered by awk, execute the copy command on each file individually.
# 5  
Old 09-24-2008
Quote:
Originally Posted by Legend986
As of now, I am using a scripting language like php to achieve this. Any advice is greatly appreciated.
if you are using PHP, here's an example
Code:
<?php
    $arr=array();
    $destination = '/dest';
    $directory = '/src';
    foreach ( glob("rib*gz" ) as $filename ) {
        echo "filename: $filename\n";
        $split = explode(".",$filename);
        if (array_key_exists($split[1],$arr) ) {
            continue;
        }else {
            $arr[$split[1]]=$filename;
        }
    }
    foreach ( $arr as $k=>$v ){
        if (! copy("$directory/$v","$destination/$v")) {
            echo "Cannot copy";
        }
    }
?>

# 6  
Old 09-26-2008
Thank You so much. You made my day! Great explanation! Also, thanks ghostdog... I was actually trying to achieve the same in php but your logic seems much better than mine... thanks again...
# 7  
Old 09-28-2008
Very Helpful and interesting....
Thanks a lot for the efforts
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List the file names available on FTP server before selecting the required file

Below is my script code.which shows the environment name and then fetch the file from the ftp server but I am facing one issue.The script should be run in both way.We can pass the arguments with script and select the environment name then file name.Here the issue is I am not able to list the files... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

2. UNIX for Dummies Questions & Answers

Selecting the file of latest Date

Hi Folks, I have one query that there is a folder in which daily several logs files are getting created , I reached to that location through putty but what I observer that 10 files of different date are been created with same name , what I need to see is the latest file ...let say the location is ... (5 Replies)
Discussion started by: KAREENA18
5 Replies

3. Shell Programming and Scripting

Selecting files from a list and passing them to a program using csh

I have a list of files, example as shown below n02-z30-dsr65-ndelt0.25-varp0.002-4x3drw-csq.msf n02-z30-dsr65-ndelt0.25-varp0.004-4x3drw-csq.msf n02-z30-dsr65-ndelt0.25-varp0.006-4x3drw-csq.msf n02-z30-dsr65-ndelt0.25-varp0.008-4x3drw-csq.msf... (8 Replies)
Discussion started by: kristinu
8 Replies

4. Shell Programming and Scripting

Selecting lines of a file

Say I wanted to select the 5th line of a file without knowing the context of the file. Would I use grep and pipe it into wc or is there a more simple way of doing this? (3 Replies)
Discussion started by: puttster
3 Replies

5. Shell Programming and Scripting

Cut a file in two file selecting a row

I all, i've got this file: 163.162.126 MGW1 163.162.126 MGW2 163.162.126 MGW3 163.162.126 MGW4 163.162.135 RNC3 163.162.135.RNC23 163.162.126. HLR Node A 163.162.126.HLR Node B 163.162.126.HLR Cluster 163.162.126.UMSC3 Node A 163.162.126.UMSC3 Node B 163.162.126.UMSC3... (6 Replies)
Discussion started by: marimovo
6 Replies

6. Shell Programming and Scripting

Selecting Lines on text file

Hi All, I am creating a script that sends log data from text files to a Database and I will like to read sugestions, as I think that there might be better ways to achive this than with my shell script; maybe perl or I don't know, but I will like to read some sugestions. The log is from... (10 Replies)
Discussion started by: oconmx
10 Replies

7. Shell Programming and Scripting

Selecting certain times from a list

Hi all, I have a list of times: ...10:02 15:34 20:05 01:51 06:55 09:00 05:52... That's just part of the list (its huge). How do I go about selecting certain times, e.g. just between 23:00 and 05:00 ?? (4 Replies)
Discussion started by: mikejreading
4 Replies

8. UNIX for Dummies Questions & Answers

Selecting Unique Values from many List

I have a question I have like 19 different list which contains the name of the server but what I need is just unique ones. First thing I need to do is just make a unique list within the list itself i.e. delete anything that is repeated inside the list like for example in list1 i... (1 Reply)
Discussion started by: pareshan
1 Replies

9. Shell Programming and Scripting

selecting each paragraph and put it into a file...help me

Dear Friends, I need a shell script. I am facing a problem while selecting the text that is between start and end tags. and insert the paragraph into a file1, next paragraph in file2...... experts please help. i have a file which contains, -------------- <abc> 111some text some text some... (2 Replies)
Discussion started by: swamymns
2 Replies

10. Shell Programming and Scripting

Selecting records from file on criteria.

Can I have 2 files as in input to the awk command? Situation is somewhat below, File A contains number & value delimited by a space. File B contains number as a part of a line. I am not supposed to retrieve more than 1 number from a line. If number from file B matches with number from... (7 Replies)
Discussion started by: videsh77
7 Replies
Login or Register to Ask a Question