Gui O
Gui O

Reputation: 383

Perl & Bash : FIND regexp

here's my problem :

I have a perl script that searches some Linux files for me. File names are like this :

shswitch_751471_126.108.216.254_13121

the problem is that

13121

is an id randomly increased. I'm trying, since this morning to search for the right regexp, but i can't find it! Please, could you help?

Here's what i have :

#!/usr/bin/perl 
$dir = "/opt/exploit/dev/florian/scan-allied/working-dir/";
$adresse ="751471" ; 
$ip =  "126.108.216.254";
$tab=`find $dir -type f -name \"$dir_$adresse_$ip_*\"`; 
print $tab;

i even tried

    $tab=`find $dir -type f -name \"$dir_$adresse_$ip_[0-9]{1}\"`;

But perl won't listen to me :(

Upvotes: 1

Views: 975

Answers (3)

TrueY
TrueY

Reputation: 7610

Uhhh. If You use then You do not really need to call find(1)! If You use File::Find module then You can have an even better find without the external call. Try something like this:

#!/usr/bin/perl

use strict;
use warnings;
use File::Find;

my $dir = "/opt/exploit/dev/florian/scan-allied/working-dir/";
my $addresse ="751471" ; 
my $ip =  "126.108.216.254";
my $re = "shswitch_${addresse}_${ip}_\d+";

sub wanted {
    /^$re$/ and -f $_ and print "$_\n";
}

find \&wanted, $dir;

This will print all matching files.

You can use find2perl utility to convert a complete find command line to the wanted function!

For

find2perl /opt/exploit/dev/florian/scan-allied/working-dir -type f -name \"shswitch_751471_126.108.216.254_${ip}_*\"

The following code is presented:

#! /usr/bin/perl -w
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
        if 0; #$running_under_some_shell

use strict;
use File::Find ();

# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

sub wanted;



# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '/opt/exploit/dev/florian/scan-allied/working-dir');
exit;


sub wanted {
    my ($dev,$ino,$mode,$nlink,$uid,$gid);

    (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
    -f _ &&
    /^"shswitch_751471_126\.108\.216\.254__.*"\z/s
    && print("$name\n");
}

Upvotes: 1

devnull
devnull

Reputation: 123608

The problem is that you have included $dir in the filename being passed to find.

You perhaps wanted to say:

$tab=`find $dir -type f -name \"shswitch_${adresse}_${ip}_*\"`; 

Upvotes: 2

Toto
Toto

Reputation: 91508

change this line:

$tab=`find $dir -type f -name \"$dir_$adresse_$ip_*\"`; 

with

$tab=`find $dir -type f -name \"${dir}_${adresse}_${ip}_*\"`; 

Upvotes: 2

Related Questions