command for script

Discussion in 'Perl Misc' started by jan09876@hotmail.com, Oct 3, 2007.

  1. Guest

    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;
    }
     
    , Oct 3, 2007
    #1
    1. Advertising

  2. Dr.Ruud Guest

    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."
     
    Dr.Ruud, Oct 3, 2007
    #2
    1. Advertising

  3. Jan Pluntke Guest

    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
     
    Jan Pluntke, Oct 5, 2007
    #3
  4. 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.


    Maybe you have transferred this script back and forth via a machine that
    uses ancient CRLF line terminators (abandoned shortly after stone
    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
    command of that name on your system.
    Use whatever means you have at your disposal to get rid of the CR
    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
    line), then type ":set fileformat=unix<Enter>", then ":wq<Enter>".

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (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
     
    Josef Moellers, Oct 5, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. San Diego Guy
    Replies:
    0
    Views:
    551
    San Diego Guy
    Aug 7, 2003
  2. Lucas Cowald
    Replies:
    4
    Views:
    1,078
    Tohid
    Oct 23, 2003
  3. Bernd Oninger
    Replies:
    1
    Views:
    795
    Martin Honnen
    Jul 7, 2004
  4. Rajat
    Replies:
    3
    Views:
    705
    Jorgen Grahn
    Jan 8, 2010
  5. VYAS ASHISH M-NTB837
    Replies:
    2
    Views:
    565
    Jan Kaliszewski
    Jan 7, 2010
Loading...

Share This Page