I am having trouble trying to find an expression that will match a word that starts with "b" ends with "t" and contains "o". The following expression grabs something that begins with "b" and ends with "t" but doesn't account for "o" ^[Bb]..t$ I understand that having brackets next to each other[][] will match the next letter in a word. I just can't figure out how I would look for say a letter five characters down. Any help would nice.
#!/usr/bin/perl
if ($#ARGV != 3 ) {
die "Requires 4 arguments";
}
my $begin = $ARGV[0];
my $letter = $ARGV[1];
my $end = $ARGV[2];
open (INFILE, $ARGV[3])
or die "Couldn't read from $ARGV[3]";
while (my $line = <INFILE>) {
my @matches=($line=~ /(\b(?i)$begin[a-z]*$letter[a-z]*$end\b)/gim);
for my $word (@matches) {
print $word, "\n";
}
}
close INFILE;
# echo "foo bar gaz bur bum bear" | grep -o "\bb[^ ]*a[^ ]*r\b"
bar
bear
# rpm -q grep
grep-2.6.3-6.el6.x86_64
(The newline between matched words is semi-surprising.)
The "\bb[^ ]*a[^ ]*r\b" contains:
\b word boundary
b literal 'b'
[^ ]* 0 or more non-space
a literal 'a'
[^ ]* 0 or more non-space
r literal 'r'
\b word boundary
Apparently the el6 grep takes the \b without the perl-flag.
Positional parameter substitution and escape characters with BASH script ... you already had the hang of them.