By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,179 Members | 970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,179 IT Pros & Developers. It's quick & easy.

improrting more than one file and matching

100+
P: 123
Hey all,
I have one folder in our shared document drive .
In that folder , there are text files(Included time stamps) like systems_14475383.txt
and systems_15354781etc
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,
Jul 10 '08 #1
Share this Question
Share on Google+
4 Replies


nithinpes
Expert 100+
P: 410
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
Jul 11 '08 #2

100+
P: 123
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\document\IP
Other directories I can open , But how can I open the above one..
Thanks
Jul 11 '08 #3

nithinpes
Expert 100+
P: 410
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\document\IP
Other directories I can open , But how can I open the above one..
Thanks
Use:
Expand|Select|Wrap|Line Numbers
  1. opendir(DIR,"\\\\IP\\cde\\groups\\document") or die "cannot open directory:$!"; 
  2.  
If you face any difficulty again, post your code and also any error message that you are getting, for further assistance.
Jul 14 '08 #4

100+
P: 123
Use:
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($matches) 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
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  $dirname = "/home/ajd/ip";
  3. if($handle = opendir($dirname))
  4. {
  5.     while(false !== ($filename = readdir($handle))){
  6.         if (is_readable($filename)) {
  7.   if ($handle1 = fopen($filename, "r") ) {
  8.                 while(!feof($handle1)) {
  9.                     $lines = fgets($handle1);
  10. if(preg_match('/ Host Name . . . . . . . . . . . . :(.*)$/',$lines,$matches))
  11.  {
  12.        print_r($matches[1]);
  13.         echo ",";
  14.  }
  15.  
  16.  
  17. if(preg_match('/IP Address. . . . . . . . . . . . : 10.(.*)$/',$lines,$matches))
  18. {  $ad = "10.";
  19.  print_r($ad.$matches[1]); 
  20. }
  21. if(preg_match('/Physical Address. . . . . . . . . :(.*)$/',$lines,$matches))
  22.  {
  23. print_r($matches[1]);
  24. echo ",";
  25.  
  26. }
  27. }                fclose($handle1);
  28.             } } } 
  29.  
  30.     closedir($handle);
  31. }
  32. ?>
  33.  
and the result I get is file1hosname,file1physicaladd,file1'sIp,file2'shos tname.........
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
Jul 14 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.