On Tuesday 02 May 2006 12:25, 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
>> --
> Timothy Murphy
> e-mail (<80k only): tim /at/ birdsnest.maths.tcd.ie
> tel: +353-86-2336090, +353-1-2842366
> s-mail: School of Mathematics, Trinity College, Dublin 2, Ireland
Hi,
Well... you've asked for something simple... Really quick hack, you need to
'update' it probably if you want to use it in some real script.
Contents of foo.pl:
#!/usr/bin/perl
open(IN, $ARGV[0]) or die("No such file: $ARGV[0]");
while($line = <IN>) {
if ($line =~ /\%n $ARGV[1]$/) {
print $line; #print this line
$line = <IN>;
print $line; #and the next one - but you probably should be looking
for the next %t token...
}
}
close(IN);
Contents of data.txt:
%n Tom
%t 3
%n Peter
%t 17
And now execute:
zabuchy% ./foo.pl test.txt Tom
%n Tom
%t 3
I hope that was what you meant....
cheers
Tomek
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!