| [reply] [d/l] [select] |
$ perl -n -e 'print if $_ eq "PEAR\n"' infile > outfile
or
$ perl -n -e 'print if /^PEAR$/' infile > outfile
The part in single quotes (the argument to the -e option) is the Perl script.
The -n option is equivalent to putting while (<>) { ... } around the code.
$_ eq "PEAR\n" or /^PEAR$/ tests if the current line (the default variable $_ here) holds your search string, where $_ =~ /.../ is implicitly assumed in the second case, i.e. a regular expression is by default matched against $_. | [reply] [d/l] [select] |
Programs of the file → processing → file can easily been written using <>
#!/usr/bin/perl
use strict;
use warnings;
while (<>) {
chomp;
...;
print ...;
}
The program would be used as follows:
perl script.pl infile > outfile
The line is present in variable $_. Check it and skip the print when it's not the value you want.
| [reply] [d/l] [select] |
| [reply] |
word = ARGV.shift
File.open( ARGV.shift, 'r' ) do |in|
File.open( ARGV.shift, 'w' ) do |out|
in.each do |line|
if line.chomp! == word
out.puts line
end
end
end
end
Just do that in Perl and Bob's your paternal male sibling.
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [d/l] |
#!/usr/bin/perl -l
my $word = shift;
open my $in, '<', shift or die $!;
open my $out, '<', shift or die $!;
while (<$in>)
{
print $out $_ if $_ eq $word;
}
close $in;
close $out;
Howdy, Uncle Bob! Nice error checking!
print map{substr'hark, suPerJacent other l',$_,1}(11,7,6,16,5,1,15,18..23,8..10,24,17,0,12,13,3,14,2,4);
| [reply] [d/l] [select] |
$ ruby -e 'File.open( ARGV.shift, "r" ) { |f| puts f.readlines }' nott
+here
-e:1:in `initialize': No such file or directory - notthere (Errno::ENO
+ENT)
from -e:1:in `open'
from -e:1
And if you're going to be that pedantic you might want to check for the error from print when you try and print to a filehandle you've opened for reading . . .
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [d/l] [select] |