Perl search in a string for....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl search in a string for....
# 1  
Old 02-16-2009
Perl search in a string for....

ok so what I am trying to do is search through 200k files that have ext .000 or .702. for *@yahoo.com.tw and if it finds that in the file. then remove the file. this is my code... what am i doing wrong. it seams it will only find asdflkajsdf@yahoo.com.tw as a string and not *@yahoo.com.tw so it will not rm the file.

#!/usr/bin/perl

# This will recursively look for files with extension .702 or .000, starting
# at the path specified ./phil /var/files/i/hate
# if yahoo.tw if found in any of these files, the file will be removed
# try it a few times, then uncomment the rm line to make it really go to work

use strict;
use warnings;

my @file_extensions = ('702', '000');
my $search_string = "yahoo.com.tw";

my $match;

if ($ARGV['0'] ne "")
{
foreach (@file_extensions)
{
my @bad_files = `find $ARGV['0'] -name \\*\.$_`;
foreach (@bad_files)
{
print "Evaluating $_\n";
$match = system("grep -i $search_string $_");
if ($match == 0)
{
#`rm -f $_`;
print "Removed $_\n";
}
}
}
}
else
{
print "I need to know where you want me to start files.... \n
IE:./phil /var/studpid_files/";
}
# 2  
Old 02-16-2009
Untested code, use at your own risk.

Code:
#!/usr/bin/perl

# This will recursively look for files with extension .702 or .000, starting
# at the path specified ./phil /var/files/i/hate
# if yahoo.tw if found in any of these files, the file will be removed
# try it a few times, then uncomment the rm line to make it really go to work

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

my $file_extensions = '702|000';
my $search_string = "yahoo.com.tw";
my $dir = $ARGV[0] or do {
   print "Usage: I need to know where you want me to start files. ie: ./phil/var/studpid_files/\n";
   exit(0);
};

find (\&wanted, $dir);

sub wanted {
   if (/\.$file_extensions\z/) {
      open (my $IN , "<", $_) or do {
         print "Can't open file $_ : $!\n";
         return(0);
      };
      my $bad = 0;
      while (my $line = <$IN>) {
         if ($line =~ /([\w.-]+\@yahoo\.com\.tw)/i) {
            print "Found $1 in file $_\n";
            $bad = 1;
            last;
         }
      }
      close($IN); 
      if ($bad) {
         sleep(1);#give it a second to close the file
         # uncomment next 4 lines when you are sure it looks like it is working
#           unlink($_) or do {
#              print "Can't delete file $_ : $!\n";
#              return(0);
#           };
      }
   }
}


Last edited by KevinADC; 02-16-2009 at 06:29 PM..
# 3  
Old 02-16-2009
The search works and returns the correct value. how ever when i try and remove the # marks and delete it no such file or directory... i know it is looking now for the name it just found and remove it .. but i need it to return the name of the file to remove. That yahoo... was found in. Thanks for the help

Edit.. Looks like the in File is not giving the correct info . it is returning the name that was found.

Quote:
Originally Posted by KevinADC
Untested code, use at your own risk.

Code:
#!/usr/bin/perl

# This will recursively look for files with extension .702 or .000, starting
# at the path specified ./phil /var/files/i/hate
# if yahoo.tw if found in any of these files, the file will be removed
# try it a few times, then uncomment the rm line to make it really go to work

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

my $file_extensions = '702|000';
my $search_string = "yahoo.com.tw";
my $dir = $ARGV[0] or do {
   print "Usage: I need to know where you want me to start files. ie: ./phil/var/studpid_files/\n";
   exit(0);
};

find (\&wanted, $dir);

sub wanted {
   if (/\.$file_extensions\z/) {
      open (my $IN , "<", $_) or do {
         print "Can't open file $_ : $!\n";
         return(0);
      };
      my $bad = 0;
      while (my $line = <$IN>) {
         if ($line =~ /([\w.-]+\@yahoo\.com\.tw)/i) {
            print "Found $1 in file $_\n";
            $bad = 1;
            last;
         }
      }
      close($IN); 
      if ($bad) {
         sleep(1);#give it a second to close the file
         # uncomment next 4 lines when you are sure it looks like it is working
#           unlink($_) or do {
#              print "Can't delete file $_ : $!\n";
#              return(0);
#           };
      }
   }
}

# 4  
Old 02-17-2009
Your comments were not very clear, but see if this helps:

Code:
# This will recursively look for files with extension .702 or .000, starting
# at the path specified ./phil /var/files/i/hate
# if yahoo.tw if found in any of these files, the file will be removed
# try it a few times, then uncomment the rm line to make it really go to work

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

my $file_extensions = '702|000';
my $search_string = "yahoo.com.tw";
my $dir = $ARGV[0] or do {
   print "Usage: I need to know where you want me to start files. ie: ./phil/var/studpid_files/\n";
   exit(0);
};

find (\&wanted, $dir);

sub wanted {
   if (/\.$file_extensions\z/) {
      open (my $IN , "<", $File::Find::name) or do {
         print "Can't open file $File::Find::name : $!\n";
         return(0);
      };
      my $bad = 0;
      while (my $line = <$IN>) {
         if ($line =~ /([\w.-]+\@yahoo\.com\.tw)/i) {
            print "Found $1 in file $File::Find::name\n";
            $bad = 1;
            last;
         }
      }
      close($IN); 
      if ($bad) {
         sleep(1);#give it a second to close the file
         # uncomment next 4 lines when you are sure it looks like it is working
#           unlink($File::Find::name) or do {
#              print "Can't delete file $File::Find::name : $!\n";
#              return(0);
#           };
      }
   }
}

I use $File::Find::name instead of $_. $File::Find ::name will be the full path to the file with the filename on the end.
# 5  
Old 02-17-2009
That worked thanks for the help .. that was driving me nuts! Smilie
# 6  
Old 02-17-2009
You're welcome
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Loop with Perl (string search)

I am using a perl script to reverse and complement sequences if a string is found. The script works as expected as standalone but I would like to use it in my bash file. However, I am not getting my expected result. My test.txt file >Sample_72... (8 Replies)
Discussion started by: Xterra
8 Replies

2. UNIX for Dummies Questions & Answers

Search different string using perl

Hello, I want to search two strings in a file and print the same in the new file using perl script. Can anyone suggest me how to do this... The file looks like below: <UML:ModelElement.requirement> <UML:Dependency name="Row_MainColumn_FW_0009"> <UML:ModelElement.taggedValue>... (3 Replies)
Discussion started by: suvendu4urs
3 Replies

3. Shell Programming and Scripting

perl- read search and replace string from the file

Dear all, I have a number of files and each file has two sections separated by a blank line. At the top section, I have lines which describes the values of the alphabetical characters, # s #; 0.123 # p #; 12.3 # d #; -2.33 # f #; 5.68 <blank line> sssssss spfdffff sdfffffff Now I... (4 Replies)
Discussion started by: sasharma
4 Replies

4. Shell Programming and Scripting

perl search string for cut data

perl -lne '$/="1H1XXXXX";print $_ if /0001|0002|0003/' data.txt> output.txt more data.txt 1H1XXXXX|0001|Y| aaa bbb ccc 1H1XXXXX|0005|N| bbb g 1H1XXXXX|0001|Y| hhh ddd 222 1H1XXXXX|0002|Y| 444 1H1XXXXX|0002|N| 222 1H1XXXXX|0003|Y| hhhh (3 Replies)
Discussion started by: kittiwas
3 Replies

5. Shell Programming and Scripting

search of string from an array in Perl

Hi All I want to search a string from an array in Perl. If a match occurs, assign that string to a variable else assign 'No match'. I tried writing the script as follows but it's in vain. Please help me.. #!/usr/bin/perl use strict; my $NER; my @text=("ORG","PER"); ... (4 Replies)
Discussion started by: my_Perl
4 Replies

6. Shell Programming and Scripting

How to search a date format from a file an replace with a string in PERL

I am very new to Perl. I am struggling so hard to search a date (such as 10/09/2009, 10-09-2009) from a text file and replace with a string (say DATE) using Perl. Please help me out. Thanks in advance. Regds Doren (4 Replies)
Discussion started by: my_Perl
4 Replies

7. Shell Programming and Scripting

search for a string -perl

Hi, I have a line where i need to get certain part of it.. example.. text txt tt: 1909 thats how exactly it looks and all spaces are to be counted.. i need to retrieve 1909.. Thanks (11 Replies)
Discussion started by: meghana
11 Replies

8. Shell Programming and Scripting

Perl: Search for string on line then compare numbers!

Hi All, I have a file that I need to be able to find a pattern match on a line, take the number on that line check if its >0.9 or <0.1 and if this is true write the line to output.out file. An example of 4 lines in my file is: 1. driver.I177.I11.net010 1.48622200477273e-05 2.... (2 Replies)
Discussion started by: Crypto
2 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

Perl: Search for string then parse next line

Hi All, I have a file that I need to be able to find a pattern match on one line then parse data on the next or subsequent lines - I will know which line needs to be parsed beforehand. This is what I currently have: while (<COMMAND_OUT>) { if ($_ =~ m/TEST/) { ... (4 Replies)
Discussion started by: pondlife
4 Replies
Login or Register to Ask a Question