how to read inputs from file for perl script?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • senthilkumars
    New Member
    • Feb 2008
    • 11

    how to read inputs from file for perl script?

    i am running my perl script as
    "perl script.pl /root/sample/shadow .htpasswd"
    now i need to pass these inputs in separate file
    and read from that file.

    for eg
    content of file may be
    /root/sample/shadow .htpasswd
    /root/sample/shadow .htpasswd1
    /root/sample/shadow .htpasswd2
    /root/sample/shadow .htpasswd3

    and my script is for comparing two files listed above.
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    I don't understand your question

    Comment

    • nithinpes
      Recognized Expert Contributor
      • Dec 2007
      • 410

      #3
      If you are trying to compare two files, you can send the path of two/more files as arguments to the script separated by space directly :
      [CODE=text]
      perl script.pl "/root/sample/shadow .htpasswd" "/root/sample/shadow .htpasswd1" "/root/sample/shadow .htpasswd2"
      [/CODE]

      Inside thescript, you can access these files through @ARGV. For ex:
      [CODE=text]
      open(F1,"$ARGV[0]") or die "failed to open first file";
      open(F2,"$ARGV[1]") or die "failed to open second file";

      while(<F1>)
      {
      ### read through first file line by line
      }

      [/CODE]

      Comment

      • senthilkumars
        New Member
        • Feb 2008
        • 11

        #4
        Originally posted by nithinpes
        If you are trying to compare two files, you can send the path of two/more files as arguments to the script separated by space directly :
        [CODE=text]
        perl script.pl "/root/sample/shadow .htpasswd" "/root/sample/shadow .htpasswd1" "/root/sample/shadow .htpasswd2"
        [/CODE]

        Inside thescript, you can access these files through @ARGV. For ex:
        [CODE=text]
        open(F1,"$ARGV[0]") or die "failed to open first file";
        open(F2,"$ARGV[1]") or die "failed to open second file";

        while(<F1>)
        {
        ### read through first file line by line
        }

        [/CODE]
        i need to give all the inputs in one file and read from that file.
        u said to pass all arguments simultaneously, but i want to write those argument
        in a file read from that.

        Comment

        • nithinpes
          Recognized Expert Contributor
          • Dec 2007
          • 410

          #5
          Originally posted by senthilkumars
          i need to give all the inputs in one file and read from that file.
          u said to pass all arguments simultaneously, but i want to write those argument
          in a file read from that.
          In that case, you have to run your original script from another script.
          Write another script (e.g new.pl), and run the script as below:

          perl new.pl paths.txt

          where paths.txt is file containing filenames. Inside new.pl, you call the other script.

          [CODE=text]

          while(<>) ## read from file line by line(one file at a time)
          {
          `perl script.pl "$_" ` ;
          ## $_ is put between " " to include paths containg spaces in between. Not required otherwise
          }
          [/CODE]

          There may be better ways of doing it. Hope, this is what you wanted.

          Comment

          • senthilkumars
            New Member
            • Feb 2008
            • 11

            #6
            Originally posted by nithinpes
            In that case, you have to run your original script from another script.
            Write another script (e.g new.pl), and run the script as below:

            perl new.pl paths.txt

            where paths.txt is file containing filenames. Inside new.pl, you call the other script.

            [CODE=text]

            while(<>) ## read from file line by line(one file at a time)
            {
            `perl script.pl "$_" ` ;
            ## $_ is put between " " to include paths containg spaces in between. Not required otherwise
            }
            [/CODE]

            There may be better ways of doing it. Hope, this is what you wanted.
            i can't get it clearly.
            As u said paths.txt i wrote like
            /root/sss/shadow /root/.htpasswd
            /root/sss/shadow /root/.htpasswd1
            /root/sss/shadow /root/.htpasswd2

            then new.pl
            [CODE=perl]#!usr/bin/perl
            while(<>)
            {
            `perl script.pl "$_" ` ;
            }[/CODE]

            but i get error like
            cannot open: No such file or directory in script.pl at line 2.
            Last edited by eWish; Feb 15 '08, 02:43 PM. Reason: Please use [CODE][/CODE] tags

            Comment

            • nithinpes
              Recognized Expert Contributor
              • Dec 2007
              • 410

              #7
              That may be because you are not chopping newline from the input,remove the trailing new line and try.
              new.pl
              [CODE=perl]
              #!usr/bin/perl
              while(<>)
              {
              chomp;
              `perl script.pl "$_" ` ;
              }

              [/CODE]

              Comment

              • senthilkumars
                New Member
                • Feb 2008
                • 11

                #8
                Originally posted by nithinpes
                That may be because you are not chopping newline from the input,remove the trailing new line and try.
                new.pl
                [CODE=perl]
                #!usr/bin/perl
                while(<>)
                {
                chomp;
                `perl script.pl "$_" ` ;
                }

                [/CODE]
                again i am getting the same problem.

                Comment

                • nithinpes
                  Recognized Expert Contributor
                  • Dec 2007
                  • 410

                  #9
                  Originally posted by senthilkumars
                  again i am getting the same problem.
                  Then, it means file is not present in that location/ it is inaccessible. The error reported is in script.pl, not new.pl

                  Comment

                  • senthilkumars
                    New Member
                    • Feb 2008
                    • 11

                    #10
                    Originally posted by nithinpes
                    Then, it means file is not present in that location/ it is inaccessible. The error reported is in script.pl, not new.pl

                    hi boss now i am getting the output correctly.
                    i forgot to remove " " in new.pl. after removed it i
                    got the output.
                    thnx for ur help.

                    Comment

                    • nithinpes
                      Recognized Expert Contributor
                      • Dec 2007
                      • 410

                      #11
                      Originally posted by nithinpes
                      Then, it means file is not present in that location/ it is inaccessible. The error reported is in script.pl, not new.pl
                      In what format are those files in?How are you trying to open the file inside script.pl?
                      If you post your code, it would be helpful to identify..

                      Comment

                      • nithinpes
                        Recognized Expert Contributor
                        • Dec 2007
                        • 410

                        #12
                        Oops! We both posted message at the same time. Ok then.

                        Happy coding !!

                        Comment

                        • KevinADC
                          Recognized Expert Specialist
                          • Jan 2007
                          • 4092

                          #13
                          I am still not even sure what the OP is doing, but it seems rather odd to shell out to perl from a perl script:

                          `perl script.pl "$_" ` ;

                          Comment

                          • senthilkumars
                            New Member
                            • Feb 2008
                            • 11

                            #14
                            Originally posted by KevinADC
                            I am still not even sure what the OP is doing, but it seems rather odd to shell out to perl from a perl script:

                            `perl script.pl "$_" ` ;
                            I got OP by removing " " in above statement. since there is no space in my
                            file path no need of putting " ".

                            Comment

                            • KevinADC
                              Recognized Expert Specialist
                              • Jan 2007
                              • 4092

                              #15
                              Originally posted by senthilkumars
                              I got OP by removing " " in above statement. since there is no space in my
                              file path no need of putting " ".

                              Thats fine, but shelling out to a perl program from within a perl program seems totally unecessary.

                              Comment

                              Working...