Writing to a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Studentmadhura05
    New Member
    • Nov 2006
    • 25

    Writing to a file

    Hi,
    I am trying to simply write the results of my grep etc to a file.
    According to what I read in books, I need to open the ouput file with ">" to overwrite or ">>" to append. And then use that filehandle in the print command
    open (IN , "$file") or die "unable to open file";

    Code:
    #Find the lines in the file containing word 'hardware' #
    while (<IN>) {
            if ($num < 30 ) {
            @hardware_line = grep (/hardware/, $_);
            print "@hardware_line\n";
        }
    
    #-----------------------------------------------------#
    # Get just the hardware address from the whole line in the file
    #-----------------------------------------------------#
         foreach my $word (@hardware_line) {
             @hardware1 = split /\s+/, $word;
    [B]         $hardware = $hardware1[3];[/B]
             open (OUT, ">$out_file1") or die "unable to open output file";
             print OUT $hardware;
             close OUT or die "$!";
         }
    I am gettng following error.

    Use of uninitialized value in concatenation (.) or string at search_IP_02.pl x for line in BOLD
    unable to open output file at search_IP_02.pl x line for line in BOLD

    Any comments?
    M
  • GunnarH
    New Member
    • Nov 2006
    • 83

    #2
    Did you read the error message before you posted here? It tells you pretty clearly what the problem is...

    Another thing is that this part of your code makes little sense to me:
    Code:
    while (<IN>) {
            if ($num < 30 ) {
            @hardware_line = grep (/hardware/, $_);
    • What purpose does the $num variable serve?
    • Why are you using the grep() function?
    • What do you think that @hardware_line contains after the while loop is finished?

    Comment

    • Studentmadhura05
      New Member
      • Nov 2006
      • 25

      #3
      1. The input file is huge and just to keep the search limited to first few (30 lines) of the file while debugging, I have used if ($num < 30) Thats the only purpose of $num. I will take it out when I am finished. (I have fogotten to paste the line $num++; and also the } for the while loop. But they exist in my code.

      2. The file contains lines like :

      hardware ethernet 00:11:23:45:67: 23

      What I am trying to do by using grep and @hardware_line is finding all such lines. And then picking up only the third element that is the actual mac address by using

      $hardware = $hardware1[3];

      3. I still do not understand the error I am getting

      Thanks
      M

      Comment

      • Studentmadhura05
        New Member
        • Nov 2006
        • 25

        #4
        I worked on the code and I understood what the error was. Thanks!

        Comment

        Working...