I have a rather simple task in trying to extract keywords from input.dat
which looks like:
func1
{
yes true;
keyword123 (1.1 0 -0.3);
gamma (0 1 0);
dir (1 0 0);
func2
{
yes false;
keyword123 (1.1 0 -0.3);
gamma (0 0 1);
dir (1 0 0);
In this case I want to get the first entry in the vector of keyword123
. If the lines containing the keyword aren't identically, the script should stop with an error. I feel that this should be a one- or twoliner in PERL using RegEx but it took me some time to get the following script:
#!/usr/bin/perl -w
use strict;
my $path = "./input.dat";
# array @grepped of lines containing keyword
my @grepped=();
open(FILE,"$path") or die "error on opening $path: $!\n";
while (my $line = <FILE>) {
if ($line=~ /keyword123/i){push(@grepped,"$line")};
}
close(FILE);
# get array @unique of unique lines
my %seen;
my @unique = grep {!$seen{$_}++} @grepped;
# continue with $unique if keyword-line is unique
my $unique;
if (scalar @unique eq 1) {
$unique=$unique[0];
} else {
die "entry not unique, exiting: $!\n";
}
# get string between brackets
$unique =~ /\((.+?)\)/;
my $final = $1;
# cut after first space
$final =~ s/\s.*//s;
print "keyword123: $final\n";
The script is giving the correct output but this can't be the best work, so any help/hint would be highly appreciated.