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
Many thanks for your help
#!/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");
open (OUT2, ">out2.txt");
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";
}
}
}
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;
}
|
|
0
|
|
|
|
Reply
|
jan09876 (16)
|
10/3/2007 5:52:45 PM |
|
In article <1191433965.875513.50640@g4g2000hsf.googlegroups.com>,
<jan09876@hotmail.com> 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
|
|
0
|
|
|
|
Reply
|
Jim
|
10/3/2007 9:28:05 PM
|
|
jan09876@hotmail.com schreef:
> A friend wrote for me this little script so I can compare two files
> and have this output.
> [...]
> I did not use the script for a while and now I am not able to find out
> what the command was.
The script has a usage section, so just start it without any parameters
(or with -h), and it will show the syntax.
(instead of -in1 I guess you need to use --in1, etc.)
--
Affijn, Ruud
"Gewoon is een tijger."
|
|
0
|
|
|
|
Reply
|
Dr
|
10/3/2007 10:42:51 PM
|
|
jan09876@hotmail.com writes:
> 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.
This text is not output anywhere in the script you pasted below, so I
believe it is the shell and not the perl program that complains
here. Do ./same.pl and /usr/bin/perl exist on your system?
Regards,
Jan
|
|
0
|
|
|
|
Reply
|
Jan
|
10/5/2007 1:25:48 PM
|
|
jan09876@hotmail.com wrote:
> A friend wrote for me this little script so I can compare two files
> and have this output.
>=20
> Common to all files:
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> Only in '1.txt':
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> Only in '2.txt':
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> 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.
Maybe you have transferred this script back and forth via a machine that =
uses ancient CRLF line terminators (abandoned shortly after stone=20
tablets cam out of fashion).
In that case the shebang line (the "#!/usr/bin/perl") will actually read =
"#!/usr/bin/perl<CR>" and it is highly unlikely that you'll have a=20
command of that name on your system.
Use whatever means you have at your disposal to get rid of the CR=20
characters, e.g.
tr -d '\015' < script > tmp.$$; mv tmp.$$ script
or open the file in vim (notice that it'll say "[DOS]" in the last=20
line), then type ":set fileformat=3Dunix<Enter>", then ":wq<Enter>".
Josef
--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
|
|
0
|
|
|
|
Reply
|
Josef
|
10/5/2007 1:37:09 PM
|
|
|
4 Replies
62 Views
(page loaded in 0.096 seconds)
Similiar Articles: AWK command inside the shell script - comp.lang.awkHi, The following awk command works fine if I use it separately. It prints 3 columns. awk 'BEGIN{FS="\t"} NR==FNR{a[$1]=$2;next} {if($1 in a) {prin... command line awk script summing values in column i - comp.lang.awk ...What's the simplest command-line awk script that would, for example, sum the values printed in column i? For example, how to add all the file size va... Script to give performance report of Solaris ? - comp.unix.admin ...Im looking for a simple script that will run these commands for me, trigger on some values and then present a simple report to me. My area of expertis is not shell ... Running a script through Automatic scheduler - comp.unix.shell ...> > Also, I have su'ed to the autosys user and have been manually able to > run all the commands. Trust me, put an env statement in a script and compare ... How can I get a Javascript command to run a Shell script? - comp ...Hi all, I need to get a shell script (on Sun Servers) run from this _javascript code and from a HTML Website: Host Overview The path is corre... How to setenv in script? - comp.unix.adminSpecifically consider this: when you run any external command (any command, whether it's a binary or script) you've implicitly created a subprocess. Running a script/Invoking a function "in the background" - comp ...I know I can invoke Matlab and ask it to execute a script passed as a command line argument, but that's not what I'm asking. > > Thanks Bash script - telnet - comp.unix.shellHi masters! I need a telnet script in bash. I need to log in a remote site for executing certain commands & tasks (remotely). Can somebody share a scr... sftp script - comp.unix.programmerHow can I get a Javascript command to run a Shell script? - comp ... sftp script - comp.unix.programmer How can I get a Javascript command to run a Shell script? - comp ... Howto script Netbackup robot inventory and master tape - comp.unix ...comp.unix.solaris - page 47 How to incrase the size ... a couple of quick scripts for Netbackup robot control: check if drive is down check if l we left a tape in a drive ... Windows Command Line Interpreter|Shell|DOS Prompt|Batch Files ...Additions and extensions to native commands. Scripts in the command line; Server 2003 tools for XP; Support tools; Batch files Batch files provide a simple way to perform many ... Scripting and Task Automation :: WinSCPSee command-line parameters to learn how to enter the console/scripting mode. For automation, commands can be read from a script file specified by /script switch ... 7/8/2012 4:34:35 PM
|