Apr 27, 2013

Loop through Directory and read the files in Perl

Now, we discuss about how to loop through a directory and read all the files in Perl
I have a directory with name "humanists" which has different files as mentioned below :

humanists :
abraham_lincon.txt
alfred_nobel.txt
mahatma_gandhi.txt
mother_teresa.txt
winston_churchill.txt

All the files has details in the following format
Name: <>
Born: <>
Died: <>

Now we have to loop thorugh all the files in the directory and get the information from these files and create a hash object.

Lets dive into this example. Please drop me a comment if you have any doubts on the same.


read_humanists.pl
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $dir_path = "/home/prabhath/prabhath_test/test/humanists";

my %humanists_list;

opendir(IN_DIR, $dir_path) or die $!; 
open (LOG_FILE, "> read_humanists.log") or die $!; 

unless ( -d $dir_path) {
   print "\n Path not present: " . $dir_path;
} else {
   print "\n Path is present: " . $dir_path;
}


while (my $file = readdir(IN_DIR)) {
    next if $file =~ /^\./;
    
    unless (-f "$dir_path/$file") {
        print "\n -W- Not a File, Ignoring : " . "$dir_path/$file" . "\n";
        next;
    }   

    open(IN_FILE, "< $dir_path/$file")
                    ||  die "\n Cant open file for reading: " . "$dir_path/$file";

    my @lines = ;
    close(IN_FILE);

    my @newlines;
    foreach my $each_line (@lines) {
        if ($each_line =~ /^Name:/) {
           my ($name) = $each_line =~ /Name:\s(.*)$/;
           $humanists_list{$file}{"name"} = $name;
        }

        if ($each_line =~ /^Born:/) {
           my ($dob) = $each_line =~ /Born:\s(.*)$/;
           $humanists_list{$file}{"date_of_birth"} = $dob;
        }

        if ($each_line =~ /^Died:/) {
           my ($dod) = $each_line =~ /Died:\s(.*)$/;
           $humanists_list{$file}{"died"} = $dod;
        }
    }   
} 

Output:
          'abraham_lincon.txt' => {
                                    'died' => 'April 15, 1865',
                                    'date_of_birth' => 'February 12, 1809',
                                    'name' => 'Abraham Lincon'
                                  },
          'winston_churchill.txt' => {
                                       'died' => 'January 24, 1965',
                                       'date_of_birth' => 'November 30, 1874',
                                       'name' => 'Winston Churchill'
                                     },
          'alfred_nobel.txt' => {
                                  'died' => 'December 10, 1896',
                                  'date_of_birth' => 'October 21, 1833',
                                  'name' => 'Alfred Nobel'
                                },
          'mother_teresa.txt' => {
                                   'died' => 'September 5, 1997',
                                   'date_of_birth' => 'August 26, 1910',
                                   'name' => 'Mother Teresa'
                                 },
          'mahatma_gandhi.txt' => {
                                    'died' => 'January 30, 1948',
                                    'date_of_birth' => 'October 2, 1869',
                                    'name' => 'Mahatma Gandhi'
                                  }
  


Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count


Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File


Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton


No comments:

Post a Comment