1844836 Members
3251 Online
110233 Solutions
New Discussion

Re: findstr analog

 
SOLVED
Go to solution
JianW
Occasional Advisor

findstr analog

Hi,
I'm looking how to set search for string in unix/dos using Perl to make it work like DOS findstr does, i.e. if string found return file name and whole line with string.

is there such command in Perl, or I need to play with m// ?

Tx
W
11 REPLIES 11
Sandman!
Honored Contributor
Solution

Re: findstr analog

Use grep inside a while loop of filenames i.e.


#!/usr/bin/sh

STRING="search string goes here"

while read f
do
grep $STRING $f && echo $STRING
done
James R. Ferguson
Acclaimed Contributor

Re: findstr analog

Hi:

# perl -ne 'print $ARGV,":",$_ if m/local/' file1 file2 ...

For example:

# perl -ne 'print $ARGV,":",$_ if m/local/' /etc/hosts /etc/services
/etc/hosts:127.0.0.1 localhost loopback
/etc/services:hacl-local 5304/tcp # HA Cluster Commands

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: findstr analog

Hi:

For re-use, you could do something as simple as:

# perl -ne 'BEGIN{$pat=shift};print $ARGV,":",$_ if m/$pat/io' pattern file1 file2 ...

Regards!

By the way, welcome to the ITRC Forums. Please consider:

http://forums1.itrc.hp.com/service/forums/helptips.do?#28

...JRF...
JianW
Occasional Advisor

Re: findstr analog

Thanks, all for help.

I'm trying it now, I think some sample won't work in scrip ?

Best
W
James R. Ferguson
Acclaimed Contributor

Re: findstr analog

Hi (again):

> I'm trying it now, I think some sample won't work in script?

Please elaborate. You can take the command line and turn it into a script like this:

# cat ./findstr
#!/usr/bin/perl
BEGIN{$pat=shift};
while (<>) {
print $ARGV,":",$_ if m/$pat/io;
}
1;

...run as (for example):

# ./findstr local /etc/hosts /etc/services

...to find the string "local", case-insensitively, in the files '/etc/hosts' and '/etc/services'.

Regards!

...JRF...
JianW
Occasional Advisor

Re: findstr analog

Tx, again James,

Probably something is different on my system, I'm using active Perl, even paste/copy of your script produce and error:
C:\PERL>perl -ne 'print $ARGV,":",$_ if m/local/' /etc/hosts /etc/services
Can't find string terminator "'" anywhere before EOF at -e line 1.

Uff, I already composed triple loop and works nicely, perl is fantastic. I just need piece of code to findstr $string in $filename

Tx again
W
James R. Ferguson
Acclaimed Contributor

Re: findstr analog

Hi (again):

Oh, running on that "other thing" (MS). Substitute double quotes for single ones and vice versa.

Regards!

...JRF...
JianW
Occasional Advisor

Re: findstr analog

Just one small thing left that killing me,
this line of script still produces an error:
print "$filename:$_\n" if m/$string/ $filename;

C:\PERL>gen72.pl
Scalar found where operator expected at C:\PERL\gen72.pl line 36, near "m/$string/ $filena
me"
syntax error at C:\PERL\gen72.pl line 36, near "m/$string/ $filename"
Execution of C:\PERL\gen72.pl aborted due to compilation errors.


what could be wrong here, both vars well defined, I'v checked them.
James R. Ferguson
Acclaimed Contributor

Re: findstr analog

Hi (gain):

> print "$filename:$_\n" if m/$string/ $filename;

...well, none of my examples used that!

I think you want (either):

print "$filename:$_\n" if m/$string/;

...which infers matching the contents of $_

(or, if $line contained a line from a file):

;print "$filename:$line\n" if $line=~m/$string/';

Regards!

...JRF...


JianW
Occasional Advisor

Re: findstr analog

Ideally I wanted to use match feeding it with file (or file name).
This example taken from you works perfectly for me, it returns all lines with match:
C:\PERL>perl -ne "print $ARGV,':',$_ if m/four/" c:\perl\etc\test3.txt
c:\perl\etc\test3.txt:four in test3
c:\perl\etc\test3.txt:four1 in test3
c:\perl\etc\test3.txt:four2 in test3 la la la la la la

I did from command line, now I tring to put this command into script and fail. I can read file into var and use =~, but still can't understand why I'm gettin this error.

Tx
W
James R. Ferguson
Acclaimed Contributor

Re: findstr analog

Hi (again):

> Ideally I wanted to use match feeding it with file (or file name).

If you mean you want to use a pattern (or token) file, like 'grep -f pattern_file file' then:

# cat .findstr
#!/usr/bin/perl
use strict;
use warnings;
my @tokens;
my @re;

die "Usage: $0 tokenfile file ...\n" unless @ARGV > 0;
my $tokenf = shift;
open( FH, "<", $tokenf ) or die "Can't open '$tokenf': $!\n";

chomp( @tokens = );
push @re, $_ for @tokens;
while (<>) {
for my $re (@re) {
if (m/$re/i) {
print "$ARGV:$_";
last;
}
}
}
1;

...put one token (pattern) to match on each line of a "token" file (for example):

# cat ./tokens
local
internet

Now, run as (for example):

# ./findstr ./tokens /etc/hosts /etc/services

Regards!

...JRF...