By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,248 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,248 IT Pros & Developers. It's quick & easy.

pattern search

P: 36
Hi can some one help with my problem

i have a script which search for the files in the directory and download it to our directroy and it has sleep to repeat the process againg after 1 hour.it compares the file in source directory , if it is not there then down load it our directory

all the comparision i am doing by grep /^$newFiles[$file2verify]/, @{$destifiles[0]}

but grep is failing when it find the following files
1)New Text Document.txt
2)New Text Document (3).txt

it is dowanloading New Text Document (3).txt for every iterations though it is already downloaded .

my guess is it failing because of (3)in the file name

can any one help me to over come this...


thanking in advance

kiran
Mar 26 '08 #1
Share this Question
Share on Google+
8 Replies


nithinpes
Expert 100+
P: 410
To avoid special meaning of parentheses when used as a pattern, you need to escape it with backslash . In this case, $newFiles[$file2verify] should be:
Expand|Select|Wrap|Line Numbers
  1. $newFiles[$file2verify]='New Text Document \(3\).txt' ;
  2.  
Mar 26 '08 #2

P: 36
To avoid special meaning of parentheses when used as a pattern, you need to escape it with backslash . In this case, $newFiles[$file2verify] should be:
Expand|Select|Wrap|Line Numbers
  1. $newFiles[$file2verify]='New Text Document \(3\).txt' ;
  2.  
Hi Thank you for your reply

actually $newFiles[$file2verify] is getting dynamically.it reads from a dynamic array.so is there any othe way ?
Mar 26 '08 #3

nithinpes
Expert 100+
P: 410
Hi Thank you for your reply

actually $newFiles[$file2verify] is getting dynamically.it reads from a dynamic array.so is there any othe way ?
Change this :
Expand|Select|Wrap|Line Numbers
  1. grep /^$newFiles[$file2verify]/, @{$destifiles[0]}
  2.  
to:
Expand|Select|Wrap|Line Numbers
  1. grep /^\Q$newFiles[$file2verify]\E/, @{$destifiles[0]}
  2.  
the variable between \Q and \E will be interpolated literally without considering any wildcards/special variables used for regular expression.
Mar 26 '08 #4

P: 36
Change this :
Expand|Select|Wrap|Line Numbers
  1. grep /^$newFiles[$file2verify]/, @{$destifiles[0]}
  2.  
to:
Expand|Select|Wrap|Line Numbers
  1. grep /^\Q$newFiles[$file2verify]\E/, @{$destifiles[0]}
  2.  
the variable between \Q and \E will be interpolated literally without considering any wildcards/special variables used for regular expression.
Hi nithinpes
thankyou for you reply

but i tried it is not working ..here is my code which i am try to do..
Expand|Select|Wrap|Line Numbers
  1. if(@invalidfiles)
  2.         {
  3.             my @updatedfiles;
  4.             foreach my $invfile(@srcFiles)
  5.             {
  6.  
  7.                 if(grep !/\Q^$invfile\E/, @invalidfiles )
  8.                 {
  9.                     #logMsg("New file $invfile Found");
  10.                     push (@updatedfiles,$invfile);
  11.  
  12.                 }
  13.             }if(@invalidfiles)
  14.         {
  15.             my @updatedfiles;
  16.             foreach my $invfile(@srcFiles)
  17.             {
  18.  
  19.                 if(grep !/\Q^$invfile\E/, @invalidfiles )
  20.                 {
  21.                     #logMsg("New file $invfile Found");
  22.                     push (@updatedfiles,$invfile);
  23.  
  24.                 }
  25.             }
  26.  
above both @invalidfiles, and @src files contains same files
[0] =download_.html
[1] = newtextdocument .txt
[2]= lates.txt
[3] = New Microsoft Excel Worksheet.xls

etc

what i am trying to do in the above code is..
i reading from a directory ,downloading it and then adding it to invalid files to avoid repeaative downloading of the same file by comparing the files.
as per your instruction it the \Q \E.

when there is no new files added in the source files...it should not add files in updatedfile, but it is happening and adding all the files in updatedfile

your help will be appreciated
Mar 27 '08 #5

nithinpes
Expert 100+
P: 410
Hi nithinpes
thankyou for you reply

but i tried it is not working ..here is my code which i am try to do..
Expand|Select|Wrap|Line Numbers
  1. if(@invalidfiles)
  2.         {
  3.             my @updatedfiles;
  4.             foreach my $invfile(@srcFiles)
  5.             {
  6.  
  7.                 if(grep !/\Q^$invfile\E/, @invalidfiles )
  8.                 {
  9.                     #logMsg("New file $invfile Found");
  10.                     push (@updatedfiles,$invfile);
  11.  
  12.                 }
  13.             }if(@invalidfiles)
  14.         {
  15.             my @updatedfiles;
  16.             foreach my $invfile(@srcFiles)
  17.             {
  18.  
  19.                 if(grep !/\Q^$invfile\E/, @invalidfiles )
  20.                 {
  21.                     #logMsg("New file $invfile Found");
  22.                     push (@updatedfiles,$invfile);
  23.  
  24.                 }
  25.             }
  26.  
above both @invalidfiles, and @src files contains same files
[0] =download_.html
[1] = newtextdocument .txt
[2]= lates.txt
[3] = New Microsoft Excel Worksheet.xls

etc

what i am trying to do in the above code is..
i reading from a directory ,downloading it and then adding it to invalid files to avoid repeaative downloading of the same file by comparing the files.
as per your instruction it the \Q \E.

when there is no new files added in the source files...it should not add files in updatedfile, but it is happening and adding all the files in updatedfile

your help will be appreciated

Kiran,
The caret symbol should be outside \Q, observe the pattern that I posted carefully. If you use it after \Q, it would lose its meaning of beginning of line.It will be matched to actual caret(^) symbol.e.g the line:
Expand|Select|Wrap|Line Numbers
  1. if(grep !/\Q^$invfile\E/, @invalidfiles )
  2.  
should be:

Expand|Select|Wrap|Line Numbers
  1. if(grep !/^\Q$invfile\E/, @invalidfiles )
  2.  
Mar 27 '08 #6

P: 36
Kiran,
The caret symbol should be outside \Q, observe the pattern that I posted carefully. If you use it after \Q, it would lose its meaning of beginning of line.It will be matched to actual caret(^) symbol.e.g the line:
Expand|Select|Wrap|Line Numbers
  1. if(grep !/\Q^$invfile\E/, @invalidfiles )
  2.  
should be:

Expand|Select|Wrap|Line Numbers
  1. if(grep !/^\Q$invfile\E/, @invalidfiles )
  2.  
hi nithin..

i tried both ways.. but still it is going inside the loop though i mentioned !
Mar 27 '08 #7

nithinpes
Expert 100+
P: 410
hi nithin..

i tried both ways.. but still it is going inside the loop though i mentioned !
I missed out one thing.The line:
Expand|Select|Wrap|Line Numbers
  1. if(grep !/^\Q$invfile\E/, @invalidfiles )
  2.  
Using ! before / is not the right-way to use for negative search. Change that to:
Expand|Select|Wrap|Line Numbers
  1. if(!(grep /^\Q$invfile\E/, @invalidfiles) )
  2.  
Mar 27 '08 #8

P: 36
I missed out one thing.The line:
Expand|Select|Wrap|Line Numbers
  1. if(grep !/^\Q$invfile\E/, @invalidfiles )
  2.  
Using ! before / is not the right-way to use for negative search. Change that to:
Expand|Select|Wrap|Line Numbers
  1. if(!(grep /^\Q$invfile\E/, @invalidfiles) )
  2.  
Wow ! its working dear..

thanks a lot....for your help.....
thank you so much
Mar 27 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.