improrting more than one file and matching

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajd335
    New Member
    • Apr 2008
    • 123

    improrting more than one file and matching

    Hey all,
    I have one folder in our shared document drive .
    In that folder , there are text files(Included time stamps) like systems_1447538 3.txt
    and systems_1535478 1etc
    That all file contains IP address . Now how can I wirte a perl script to have IP address from each of them ??
    Any Idea?
    Thanks,
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    To give you some hints; use opendir() and readdir() functions to open the folder and get the files inside it, respectively. Parse through each file using open(). Read each file and extract IP addressses using regular expression.
    Write your script and post it here if you face any issues..

    - Nithin

    Comment

    • ajd335
      New Member
      • Apr 2008
      • 123

      #3
      Originally posted by nithinpes
      To give you some hints; use opendir() and readdir() functions to open the folder and get the files inside it, respectively. Parse through each file using open(). Read each file and extract IP addressses using regular expression.
      Write your script and post it here if you face any issues..

      - Nithin
      Hey Nithin,
      I cant open the DIR at first...
      i am working on perl on /home/ajd/data/ip.pl where I am writing the script
      and the dir which I need to open is in shared folder of \\cde\groups\do cument\IP
      Other directories I can open , But how can I open the above one..
      Thanks

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Originally posted by ajd335
        Hey Nithin,
        I cant open the DIR at first...
        i am working on perl on /home/ajd/data/ip.pl where I am writing the script
        and the dir which I need to open is in shared folder of \\cde\groups\do cument\IP
        Other directories I can open , But how can I open the above one..
        Thanks
        Use:
        Code:
        opendir(DIR,"\\\\IP\\cde\\groups\\document") or die "cannot open directory:$!";
        If you face any difficulty again, post your code and also any error message that you are getting, for further assistance.

        Comment

        • ajd335
          New Member
          • Apr 2008
          • 123

          #5
          Originally posted by nithinpes
          Use:
          Originally posted by bretticus
          Where it printed "array()" 10 times means that you have 10 files in that directory but you never matched anything from your preg_match().

          Just for fun, replace print_r($matche s) with var_dump($lines ) and tell me what you get? Not sure if that gives you line by line or the whole file. Also, you may need a 'g' pattern modifier to turn on global pattern searching.

          Hi Bretticus ,
          It was a small mistake why the answer was not showing up...
          I have the below code
          Code:
          <?php
           $dirname = "/home/ajd/ip";
          if($handle = opendir($dirname))
          {
              while(false !== ($filename = readdir($handle))){
                  if (is_readable($filename)) {
            if ($handle1 = fopen($filename, "r") ) {
                          while(!feof($handle1)) {
                              $lines = fgets($handle1);
          if(preg_match('/ Host Name . . . . . . . . . . . . :(.*)$/',$lines,$matches))
           {
                 print_r($matches[1]);
                  echo ",";
           }
          
          
          if(preg_match('/IP Address. . . . . . . . . . . . : 10.(.*)$/',$lines,$matches))
          {  $ad = "10.";
           print_r($ad.$matches[1]); 
          }
          if(preg_match('/Physical Address. . . . . . . . . :(.*)$/',$lines,$matches))
           {
          print_r($matches[1]);
          echo ",";
          
          }
          }                fclose($handle1);
                      } } } 
          
              closedir($handle);
          }
          ?>
          and the result I get is file1hosname,fi le1physicaladd, file1'sIp,file2 'shostname..... ....
          Now actually , I want to store the result into one text file or execel file.(or database)...I need a way in which i can store the value of (e.g)
          array1[0] = file1's hostname
          array1[1]= file2's hostname
          ....
          array2[0] = file1's IP
          array2[1]= file2's Ip ....

          how can i do so...?
          Thanks

          Comment

          Working...