开发者

How can i Search multiple files one pattern and return the matched values from the latest file?

开发者 https://www.devze.com 2023-02-22 16:55 出处:网络
I am totally new to perl programming. Please help me to form a logic for this. I have many files in one directory, e.g. 1.txt, 2.txt, 3.txt, ..., n.txt.

I am totally new to perl programming. Please help me to form a logic for this.

I have many files in one directory, e.g. 1.txt, 2.txt, 3.txt, ..., n.txt.

Each file contains several data in this format. The ID column will be un开发者_StackOverflow社区ique along a file.

ID | Name  | Place   

1  | name1 | Chennai

2  | name2 | Mumbai

There can be change the ID -1 can repeat in different file. So I need to search each ID in all the files in the directory and write the attributes for each ID to another file. The resultant file should have unique ID's and the attributes should be from the file with latest creation date.

So BASICALLY resulting file will be a master list of ID's and the attributes.

Please help in this regard. I am only able to read the file and select it and put it in a different file. Could anyone please help me to move forward on this.

Thank you so much in Advance


Use a hash with IDs as keys and store the values from the files in timestamp order in that hash.
Here's a prototype:

my %data;

for my $file (sort { -M $a <=> -M $b } @files) {
    my @data = process_file($file); # an array of hashrefs like { id => 1, name => "name1", place => 'Chennai' }
    $data{$_->{id}} = $_ for @data;        
}  

Then store the data from the hash to a file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号