CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
need help on searching a log file with 3 dots

 



amangupta14
New User

Jul 21, 2009, 5:01 AM

Post #1 of 7 (3451 views)
need help on searching a log file with 3 dots Can't Post

I have 3 log files in a directory out of which the names of 2 log files consists of 4 or more dots. Only 1 log filename includes 3 dots eg: aman.deepak.pavan.log. How can I search for that file only(with 3 dots) and move it to a particular Folder using PERL???? Please help.


perllearner
Novice

Jul 24, 2009, 4:56 AM

Post #2 of 7 (3394 views)
Re: [amangupta14] need help on searching a log file with 3 dots [In reply to] Can't Post

Hi, Try out the below script.

#!/usr/bin/perl
use strict;

# Beginning of Program
my $var1 = 'log';
my @array=`ls -1 *.log`; # ls -1(one) gives only the file name in the current directory.

for (my $i=0;$i<$#array;$i++)
{
my $dest = "/aaaa/bbb/ccc/temp/";

if($array[$i] =~ /^[a-z]+\.[a-z]+\.[a-z]+\.$var1/)
{
exec `mv -f $array[$i] $dest`;
}
}

This code should obviously work.


FishMonger
Veteran / Moderator

Jul 24, 2009, 7:23 AM

Post #3 of 7 (3386 views)
Re: [perllearner] need help on searching a log file with 3 dots [In reply to] Can't Post

perllearner,

You need to learn about Perl's methods for retrieving the file list and moving the files instead of using non portable shell commands.


perllearner
Novice

Jul 24, 2009, 8:31 PM

Post #4 of 7 (3361 views)
Re: [FishMonger] need help on searching a log file with 3 dots [In reply to] Can't Post

Fish Monger,

Can you please guide me to know some methods of perl to retrieve the file list and to move a file.

Thanks...


FishMonger
Veteran / Moderator

Jul 25, 2009, 6:51 AM

Post #5 of 7 (3355 views)
Re: [perllearner] need help on searching a log file with 3 dots [In reply to] Can't Post

Perl has at least 3 methods to retrieve the file list.
1) glob operator
perldoc -f glob

2) opendir / readdir operators
perdoc -f opendir
perdoc -f readdir

3) < > diamond operator
I'm not sure which perldoc it's in, but try 'perldoc perldata'

I generally use the diamond operator.

To move or copy a file you'd use the File::Copy module.

In your example, you used a regex to find the desired file. That's ok, but the OP didn't give any details of the file name other than the number of dots, which means your regex may not work.

The C style for loop initialization is IMO messy. It would be cleaner to do it like this:

Code
for my $i ( 0..$#array ) {


Since the destination directory doesn't change, it would be better to move its assignment outside of the loop.

Here's a basic script I'd use, but it still needs error handling and bullet proofing.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use File::Copy;

my $dest_dir = './backup';

foreach my $log_file ( <*.log> ) {

my $dots = $log_file =~ tr/.//;

if ( $dots == 3 ) {
move($log_file, "$dest_dir/$log_file");
}
}



ichi
User

Jul 25, 2009, 7:39 AM

Post #6 of 7 (3352 views)
Re: [perllearner] need help on searching a log file with 3 dots [In reply to] Can't Post


Code
use File::Copy 'move'; 
while (<*.log>){
chomp;
@s = split /\./;
if ( scalar @s eq 4 ){
move($_,"/tmp/");
}
}



JenniC
Novice

Aug 7, 2009, 11:11 AM

Post #7 of 7 (3288 views)
Re: [amangupta14] need help on searching a log file with 3 dots [In reply to] Can't Post

    

In biterscripting,




Code
# The story of three little dots. 
var str list ; lf -n "*.log" "C:\the folder" > $list
while ($list <> "")
do
# Get the next file.
var str file ; lex "1" $list > $file
# Does $file have three dots ?
if ( { sen "^.^" $file } == 3 )
# Yes, move file.
system move $file "C:\the other folder"
endif
done



Just translate to perl. I am using "*.log" for file name pattern, "C:\the folder" as the folder where files are located, and "C:\the other folder" as the folder to which the files are moved. Use appropriate values for these.

Jenni

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives