Re: command for script



In article <1191433965.875513.50640@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
<jan09876@xxxxxxxxxxx> wrote:

A friend wrote for me this little script so I can compare two files
and have this output.

Common to all files:
====================
Only in '1.txt':
====================
Only in '2.txt':
====================
I did not use the script for a while and now I am not able to find out
what the command was. I tried different commands but the script keep
on saying: No such file or directory. I have 1.txt and 2.txt as files
that I want to compare.


./same.pl -in1 1.txt -in2 2.txt -same out1.txt -diff out2.txt

That line above is, in fact, the command you should use to run your
script, assuming that the script is named 'same.pl', resides in your
current default directory, your computer has the executable
/usr/bin/perl installed, and your input files are named '1.txt' and
'2.txt' in your current default directory. Are all of these things
true? If so, your program should work and it is not clear exactly what
you are asking.

If you are getting the error message 'No such file or directory', then
the input files you have specified on the command line do not exist.


#!/usr/bin/perl

use strict;
use Getopt::Long;

my $USAGE =
"same.pl [--in1 inputfile1 --in2 inputfile2 --same outputSameWords --
diff outputDiffWords]
Compares the 2 in files and checks the same and different words";

my %opts;
GetOptions(\%opts, qw(in1=s in2=s same=s diff=s)) || die $USAGE;

my $fs1 = $opts{in1};
my $fs2 = $opts{in2};
my $out1 = $opts{same};
my $out2 = $opts{diff};

main();
print "done!";

sub main {
open (OUT1, ">out1.txt");

The above line should be

open (OUT1, '>', $out1) or die("Can't open $out1 for writing: $!);

to 1) actually use the -same option parameter, 2) use the more reliable
3-argument version of open, and 3) check to see if the open actually
worked.

open (OUT2, ">out2.txt");

Ditto.

parse();
close OUT1;
close OUT2;
}

sub parse {
my $_words1 = getWords($fs1);
my $_words2 = getWords($fs2);
for (sort keys %$_words1) {
if (exists $_words2->{$_}) {
print OUT1 "$_\n";
}
else {
print OUT2 "$_\n";
}
}
}

This program does not check for words in file 2 that are not in file 1.
That would involve replicating the for loop above but exchanging
$_words1 and $_words2, not printing to OUT1, and opening and printing
to another output file (e.g. OUT3 => 'out3.txt').


sub getWords {
my $fs = shift;
my $txt = getFile($fs);
my %words;
while ($txt =~ m/(\w+)/g) {
$words{$1} = 1;
}
return \%words;
}

sub getFile {
my $fs = shift;
open (IN, $fs) or die "cant open $fs";
my $txt = do{local $/;<IN>};
return $txt;
}


--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
.



Relevant Pages

  • RE: Want to interactively run a shell script
    ... sub process_command { ... Want to interactively run a shell script ... Any opinion expressed in this e-mail is personal to the sender ...
    (perl.beginners)
  • Re: Newbie "undefined value"
    ... > used strict and -w. ... From that same command line, if I pass in parameters, ... > to print to a file and using parts of Stein's guestbook script. ... > sub write_datafile { ...
    (comp.lang.perl.misc)
  • Re: Cycle through variables
    ... the same command for all variables without copying pasting the command 20 ... Below is an example of my current script - I'd really ... Define objTextFile and objTextFile2 outside of the Sub ...
    (microsoft.public.scripting.vbscript)
  • Newbie "undefined value"
    ... From that same command line, if I pass in parameters, ... I thought "strict" would catch all ... I had a short script that printed to the browser. ... sub write_datafile { ...
    (comp.lang.perl.misc)
  • Re: Problems trying to configure Linux laptop to print to Windows XP shared printer
    ... map to guest = Never ... check password script = ... enumports command = ... ldap delete dn = No ...
    (comp.os.linux.setup)