- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: findstr analog
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2007 07:45 AM
08-30-2007 07:45 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2007 07:52 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2007 07:55 AM
08-30-2007 07:55 AM
Re: findstr analog
# 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2007 08:05 AM
08-30-2007 08:05 AM
Re: findstr analog
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2007 02:06 AM
08-31-2007 02:06 AM
Re: findstr analog
I'm trying it now, I think some sample won't work in scrip ?
Best
W
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2007 02:12 AM
08-31-2007 02:12 AM
Re: findstr analog
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2007 04:15 AM
08-31-2007 04:15 AM
Re: findstr analog
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2007 04:22 AM
08-31-2007 04:22 AM
Re: findstr analog
Oh, running on that "other thing" (MS). Substitute double quotes for single ones and vice versa.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2007 05:25 AM
08-31-2007 05:25 AM
Re: findstr analog
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2007 05:37 AM
08-31-2007 05:37 AM
Re: findstr analog
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2007 07:34 AM
08-31-2007 07:34 AM
Re: findstr analog
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2007 10:25 AM
08-31-2007 10:25 AM
Re: findstr analog
> 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...