On Tue, May 02, 2006 at 06:25:27PM +0100, Timothy Murphy wrote:
> Talking of scripts, could you please tell me
> the simplest script to search through "refer-like" files
> with items separated by a blank line, eg
>> %n Tom
> %t 3
>> %n Peter
> %t 17
>> I'd like a script (in any known language), say "foo", so that
> "foo Tom" would print out
>> %n Tom
> %t 3
>
Well - something like this would work (it's quick and dirty, and there
are probably cleaner / faster ways ... ):
[proinnsias at butler ~]$ cat foo.pl
#!/usr/bin/perl
my $printflag = 0 ;
my $searchstring = shift ;
while (<>) {
if ($_ =~ /$searchstring/ ) {
$printflag = 1 ;
} elsif ($_ =~ /^$/) { # Empty Line
$printflag = 0 ;
}
if ($printflag == 1) {
print $_ ;
}
}
[proinnsias at butler ~]$ cat foo
%n Tom
%t 3
%n Peter
%t 17
[proinnsias at butler ~]$ ./foo.pl Tom foo
%n Tom
%t 3
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!