Write a Perl script which will print out the pathnames of all text files which a
ID: 3805196 • Letter: W
Question
Write a Perl script which will print out the pathnames of all text files which are under any of the directories whose names are given in the files on the command line. Write a Perl script, pill. which will interleave the lines of a file with those of another file writing the result to a third hide. If the files are a different length, then the excess lines are written at the end. A sample invocation: pill filial filein.2 file out Write a perl script which will print on stout the names of all text files under any of the directories given on the command line which contain the string "sniffer". A file name should only be printed a maximum of one time.Explanation / Answer
4.Ans
#!/usr/local/bin/perl use strict;
@fname;
$i = 0;
$totCount;
open(INFILE,"dir2.dat");
while($fname[$i] = <INFILE>)
{
chop($fname[$i]);
$fname[$i] =~ s/*//g; $i++; $totCount++;
}
$error = open(OUTFILE,"> dict.html");
if(!$error)
{
print("Error "); exit(0);
}
my $dir2 = '/';
my $body;
my $saveFile = '/dev/null';
opendir2( MYDIR2, $dir2 ) or die 'opendir2';
$body =
join( " ",
map { '<a href="$_">$_</a><br>' }
sort { $a cmp $b }
grep { ! /^./ }
grep { -T "$dir2$_ }
readdir2 MYDIR2
);
closedir2 MYDIR2;
## build your link page
## using $body
open( FILE, $saveFile ) or die 'open';
print FILE <<EOF;
<HTML>
<HEAD>
<TITLE>My Files</TITLE>
<BODY>
$body
</BODY>
</HTML>
EOF
close FILE;
5.Ans
16.Ans
$Appended = 0;
if ($Appended)
{
open(MYOUTFILE, ">fname.out"); #open for write, overwrite
}
else
{
open(MYOUTFILE, ">>fname.out"); }
print MYOUTFILE "Welcome: ";
print MYOUTFILE &to " ";
print MYOUTFILE <<"Chegg ";
close(MYOUTFILE);
#!/usr5/bin/perl -w
use strict;
my $holdTerminator = $/;
undef $/;
my $buf = <STDIN>;
$/ = $holdTerminator;
my @lines = split /$holdTerminator/, $buf;
$buf = "init";
$buf = join $holdTerminator, @lines;
print $buf;
print " ";
#!/usr6/bin/perl -w
use strict;
my $bigfname = "/scratch/bfile.txt";
my $sipfname = "/scratch/sip.out";
my $afname = "/scratch/a.out";
my $slurpfname = "/scratch/slurp.out";
sub slurp()
{
my $inf;
my $ouf;
my $holdTerminator = $/;
undef $/;
open $inf, "<" . $bigfname;
my $buf = <$inf>;
close $inf;
$/ = $holdTerminator;
my @lines = split /$holdTerminator/, $buf;
$buf = "init";
$buf = join $holdTerminator, @lines;
open $ouf, ">" . $slurpfname;
print $ouf $buf;
print $ouf " ";
close $ouf;
}
sub sip()
{
my $inf;
my $ouf;
open $inf, "<" . $bigfname;
open $ouf, ">" . $sipfname;
while(<$inf>)
{
my $line = $_;
chomp $line;
print $ouf $line, " ";
}
close $ouf;
close $inf;
}
sub builda()
{
my $inf;
my $ouf;
my @a;
open $inf, "<" . $bigfname;
while(<$inf>)
{
my $line = $_;
chomp $line;
push @a, ($line);
}
close $inf;
open $ouf, ">" . $afname;
foreach my $line (@a)
{
print $ouf $line, " ";
}
close $ouf;
}
sub main()
{
my $time1 = time();
print "Starting sip ";
sip();
print "End sip ";
my $time2 = time();
print "Starting a ";
builda();
print "End a ";
my $time3 = time();
print "Starting slurp ";
slurp();
print "End slurp ";
my $time4 = time();
print "Sip time is ", $time2-$time1, " seconds ";
print "A time is ", $time3-$time2, " seconds ";
print "Slurp time is ", $time4-$time3, " seconds ";
}
main();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.