Reputation: 29
I wrote a Perl program which reads text from text file and prints it out.
I want to print out a line which has specific format.
For example, there are some lines like this:
information:
Ahmad.prn:592118:2001:7:5:/Essay
Ashford.rtf:903615:2001:6:28:/usr/Essay
Barger.doc:243200:2001:7:4:/home/dir
end of Information.
I want to read only these three lines:
Ahmad.prn:592118:2001:7:5:/Essay
Ashford.rtf:903615:2001:6:28:/usr/Essay
Barger.doc:243200:2001:7:4:/home/dir
I think that the meaning of the fields is:
Ahmad.prn <- file name
592118 <- size of file
2001:7:5 <- created date
/Essay <- path of file
My code is this:
#!/usr/bin/perl
use strict;
use warnings;
open (my $infh, "<", $file)||die "cant open";
while(my $line = <$infh>) {
chomp ($line);
if ($line =~ /(what regular expression do I have to put in here?)/) {
print "$line";
}
}
close ($infh);
Upvotes: 2
Views: 727
Reputation: 11
$line =~ ([a-zA-Z.]+):(\d+):(\d+):(\d+):(\d+):([\/A-Za-z]+)
$name = $1; #Ahmad.prn
$id = $2; #592118
$year = $3; #2001
$dir = $6; #/Essay
Note: loop through it for multiple names
Upvotes: 0
Reputation: 1
#!/usr/bin/perl
use strict;
my $inputText = qq{
Ahmad.prn:592118:2001:7:5:/Essay
Ashford.rtf:903615:2001:6:28:/usr/Essay
Barger.doc:243200:2001:7:4:/home/dir
end of Information.
};
my @input = split /\n/, $inputText;
my $i = 0;
while ($input[$i] !~ /^end of Information.$/) {
if ($input[$i] !~ /:/) {
$i++;
next;
}
my ($fileName, $fileSize, $year, $month, $day, $filePath) = split /:/, $input[$i];
print "$fileName\t $fileSize\t $month/$day/$year\t $filePath\n";
$i++;
}
Upvotes: 0
Reputation: 2247
Since you have this format for Ahmad.prn:592118:2001:7:5:/Essay
Ahmad.prn <- file name
592118 <- size of file
2001:7:5 <- created date
/Essay <- path of file
you can use this regular expression
/^\s*(\S+):(\d+):(\d+:\d+:\d+):(\S+)\s*$/
With this you will have file name in $1, Size of the file in $2, Date of creation in $3, Path to the file in $4
I added optional spaces in the start and end of the line, if you want to allow optional spaces after or before : you can add \s*
Upvotes: 0
Reputation: 123791
If lines you need always ends with /Essay, you may use following regex
/:\/Essay$/
Edit 1: looks there is middle parts are only numbers, you may match this way.
/:\d+:\d+:\d+:\d+:/
Upvotes: 3