Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find cmd works on UNIX but not LINUX in Perl script 1

Status
Not open for further replies.

ejaggers

Programmer
Feb 26, 2005
148
US
I have a Perl script that searches all files starting in the home directory for a string. i.e. file /usr/users/myhome/xzy contains the strings ‘theKing’ and ‘THEKING’.

The UNIX command:
find /usr/users/myhome -name '*' -exec grep -csi 'theking' -exec grep ls -l {} \;

Lists all files and indicates whether it found the string as follows:
/usr/users/myhome/notfound:0

/usr/users/myhome/xzy:2 where :2 says there are two matches

This script will NOT work on my LINUX box because of the above find command.
Can anyone help me with the correct find for LINUX RedHat 5.3.
Or does anyone know a better (especially faster) way to do this?

Code:
#!/usr/bin/perl
    system('clear');
    $Target = 'theKing';
    $Null   = '/dev/null';
    $Path   = $ENV{HOME}; 
   
    my $find = "find $Path -name '*'  " .
               "-exec grep -csi \'$Target\' " .
               "-exec grep ls -l {} \\;" ;

    $find    = `$find`;
  
    my ( @record ) =   split("\n",$find); 
    foreach  ( @record) {  
        next if ( /\.sh_history/ ); 
        my ( $filename, $match ) = split(':',$_); 
        next if ( ! $match );    
        print "$_\n"; 
    }
 
First of all, the "[tt]-name '*'[/tt]" is pointless. The find will match all files anyway. You don't have to tell it to match everything. Maybe change it to only look at files and not directory entries with "[tt]-type f[/tt]".

Second, each "[tt]-exec[/tt]" needs to be terminated by it's own "[tt]\;[/tt]", so you need one before the second "[tt]-exec[/tt]".

Third, what is the "[tt]-exec grep ls -l {} \;[/tt]" supposed to do? You are grepping for "[tt]ls[/tt]" in the file? It's kind of a malformed statement.

Maybe change the find to...
Code:
find /usr/users/myhome -type f -exec grep -csi 'theking' {} \; -ls
See if that helps.

 
SamBones,
I didn't write the find stmt, and can't really explain why it is that way. But the one you gave me works fine!!! The only difference is, yours does not return the count of how many times it occurs. Although I thought the
-c in the -csi would. But I can live without the count.

Thanks!!!
 
Note however that if the script is to remain portable, the -ls option is not available on most flavours of Unix.

Annihilannic.
 
No problem...
Code:
echo "Number of Kings in descending order"
echo "==================================="
find /usr/users/myhome -type f -exec grep -csi 'theking' {} \; -print | \
    while read NUM; do read FILE; echo "$NUM\t$FILE"; done | \
    sort -rn
It's technically still a one liner, but I broke the line for better readability.

You can change the "[tt]echo[/tt] to a "[tt]print[/tt]" with some nice formatting if you want it prettier.


 
Hmmm, I think I misread your response. I don't know why it's not giving you the count. It should be. You might need to escape the dash in the "[tt]-csi[/tt]" in case the shell is interpreting that.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top