Update soundtest
[grml-scripts.git] / usr_bin / find-text
1 #!/usr/bin/perl -s
2 # Written By Shamir Biton , 27/08/2001
3 # This script can search for text pattern in text file only
4
5 use Cwd;                                                # to use internal perl libreary
6 sub ScanDirectory
7 {       
8         my ($workdir) = shift;
9         my ($startdir) = &cwd;                          # $startdir will get the current directory value
10         
11         # This part of code take the list of names from $workdir and place it in @names array
12         
13         chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
14         opendir(DIR,".") or die "Unable to open dir $workdir:$!\n";
15         my @names = readdir(DIR) or die "Unable to read dir $workdir:$!\n";
16         closedir(DIR);
17         
18         #
19         foreach my $name (@names)
20         {
21                 next if ($name eq ".");
22                 next if ($name eq "..");
23                 next if ($name eq "lost+found");
24                 
25                 if (-d $name) 
26                 {
27                         &ScanDirectory($name);
28                         next;
29                 }
30                 
31                 if (-T $name)                                 # only if it is text file
32                 {
33                         # print ("Checking $name..\n");
34                         $NofTimes = 0;
35                         open(FILENAME,$name);
36                         
37                                 while(<FILENAME>)
38                                 {
39                                 
40                                         if (/$wordTOfind/i)             # key word to search
41                                         {
42                                         $NofTimes +=1;
43                                         }       
44                                 }
45                                 
46                                 close(FILENAME);
47                                 if ($NofTimes > 0) 
48                                 {
49                                         print ("Found in $workdir/$name [$NofTimes Times]\n");
50                                 }       
51                         
52                 }       
53         }
54         
55         chdir($startdir) or die "Unable to change dir $startdir:$!\n";
56 }
57
58 my ($defaultdir) = &cwd;
59 printf("Enter root path: [$defaultdir] ");
60 $userdir = <STDIN>;
61 chomp $userdir;
62
63 if (! $userdir eq "")
64 {
65         while (! -d $userdir) 
66         {
67                 printf("Wrong Directory name, Please Enter Valid path name:");
68                 $userdir = <STDIN>;
69                 chomp $userdir;
70         }                       
71 }
72 printf("Enter Text To seach: ");
73 $wordTOfind = <STDIN>;
74 chomp $wordTOfind;
75
76 if ($userdir eq "") 
77         {
78                 &ScanDirectory(".")
79         }
80 else
81         {
82         print $userdir  ;
83         &ScanDirectory($userdir);
84                         
85         }
86