die "Usage infile outfile reGex" if @ARGV != 3;
($regex) = @ARGV;
open(F,$ARGV[0]) or die "Can't open";
open(FOUT,"+>$ARGV[1]") or die "Can't open";
while (<F>)
{
print FOUT if /$regex/.../$regex/;
}
No matter what I give $regex on the command line, it will only print to FOUT the first 3 line of F. Can anyone tell me what I do wrong here. I want to print out all lines matches whatever I give $regex on the command line.
For the above code $regex get the input file name only. So you not able get the desired output. You must assign the third argument as $regex.
I slightly modify your code. Try this
die "Usage infile outfile reGex" if @ARGV != 3;
($regex) = $ARGV[2];
open(F,$ARGV[0]) or die "Can't open";
open(FOUT,"+>$ARGV[1]") or die "Can't open";
while (<F>)
{
# $_ is a default variable
# if your regex match the line you get the line into $ARGV[1] file
print FOUT "$_" if ($_ =~ /$regex/);
}