Reputation: 191
First of all, I'm a noob on perl, and I've tried to find the answer to that question on the internet but couldn't get (or understand) it... So, I have a script that scan throught a text file and writes all line except those starting with an A.
#!/usr/bin/perl
use strict;
use warnings;
open (my $file, "<", "/file.txt") or die "cannot open < file.txt $!";
while (<$file>) {
unless (/^A/) {
print;
}
}
That works. But I get the results of this script in the terminal. Now, I just want these results to be saved in a new text file. Can somebody help me ?
Thanks a lot !
Upvotes: 0
Views: 93
Reputation: 3116
Just open another file and print data to it.
#!/usr/bin/env perl
use strict;
use warnings;
my $infile = 'input.txt';
my $outfile = 'output.txt';
open my $infh, "<", $infile or die "Can't open $infile: $!";
open my $outfh, ">", $outfile or die "Can't open $outfile: $!";
while (<$infh>) {
print $outfh $_ unless /^A/;
}
close($infh);
close($outfh);
But, generally speaking, you may use some perl magic combined with shell redirection instead. The whole script will become simple oneliner:
$ perl -ne 'print $_ unless /^A/;' input.txt > output.txt
Upvotes: 2