Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl confusion 1

Status
Not open for further replies.

LAdProg2005

Programmer
Feb 20, 2006
56
US
I am new to perl programming. I am trying to write a script in perl to use certain information from the log file and save it in an output file which is comma delimited. I looked at tutorials and examples but I found nothing concret.

Does anyone know how to grep certain information from a file which is not already delimited somehow? Let me know if more information is needed.

--p
 
I can't account for that "This" with the code thats given ...

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Ok, sorry about that.

Code:
#!/usr/bin/perl


open FH, "<dhcpd.leases-060222";
open FH2, ">detail.txt";

while (<FH>) {   #read through file line by line
  if (/lease/) { #if the line contains the word lease, then
     @words=split /\s+/, $_; #get the lease variable by splitting the
     $lease=$words[1];       #line on whitepace
  }

  #do the same for other variables here
  if (/starts/){
     @words=split /\s+/, $_;
     $starts=$words[1];
  }

  if (/ends/){
     @words=split /\s+/, $_;
     $ends=$words[1];
  }

  if (/hardware ethernet/){
     @words=split /\s+/, $_;
     $ethernet=$words[1];
  }
  if (/host/) {
     @words=split /\s+/, $_;
     $host=$words[1];
     print FH2 "\"$lease\", \"$starts\", \"$ends\", \"$ethernet\" \"$host\"\n";
      #when we track the client line, output the record, and overwrite
      #the record until the client line comes around again
  }
}
close FH2;
close FH1;

[code]

I have a question also.  Help me understand how this code is read?  

if ($line =~ /host (.*) {/) {   #change mode to host
      push(@hostname,$1);
      $mode = "HOST";
      }
This does the same thing as the if (/host/) above?

Thanks so much for being patient with me...
 
Sorry I made a booooboooo!!!Actually it prints

"hjohn-1": "","","","hardware" [...] and "client-hostname", "147.126.1.32","starts","ends", "hardware"
THis in the previous post was just coming out a comment line which was wrong.

I tried comparing with if statements but most of the examples have explicit example of using delimited files and comparing through fields but in my case i don't have that :(

I tried different ways to print so i can get hjohn-1, 147.126...", 2006/02/22, 2006/02/23, 00:15:26:.. but failed horribly. I also don't know y "client-hostname" comes up at all when we aer not even trying to read it. It is the last line in lease{....}. Very weird :( Any pointers?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top