Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2003
    Posts
    31

    Smile Unanswered: printing in column format

    How can I transfer this data file using activeperl in to column format as bellow output file

    Data file contain
    Server alfa1
    Error system down

    Server alfa2
    Error network proble
    Error disk problem
    Error key board problem

    Server alfa3
    Error system down
    Error network problem

    The output file

    Server Error
    Alfa1 system down

    Alfa2 network proble
    disk problem
    key board problem

    alfa3 system down
    network problem

  2. #2
    Join Date
    Nov 2003
    Posts
    65

    Cool Re: printing in column format

    I don't know how your getting the input file but here is a small script that does what u ask from the command line Given inputfile and outfile names respectively. Just make sure the the input file is tab'ed between server and server name and error and error message. Probably not the best code in the world but its something i wrote on the fly. Let me know how it turns out.

    Code:
    if (@ARGV == 2)
    {
        $inputFile = shift @ARGV;
        open(inputFile);
        $outputName = shift @ARGV;
        $outputFile =">".$outputName;
        open (outputFile);
    }
    else
    {
        print "usage: perl -w cleanDataFile.pl inputFileName outputFileName\n";
        exit;
    }
    # ::::::::::::::::::::::::::::::::::::
    # 	Define variables.
    # ::::::::::::::::::::::::::::::::::::
    $Error = "";
    $serverOrMessage = "";
    $ErrorCount = 0;
    
    # Loop thru input file until EOF reached
    print outputFile ("Server"."\t\t"."Error"."\n");
    while (<inputFile>)
    {
        chop($_);	#remove line feed from input record
        if ( (defined($_)) && (length($_) != 0) )	
        {
    	($Error, $serverOrMessage) = split(/\t+/, $_);
    
            if (substr($Error, 0, 6) eq "Server")
            {
                print outputFile ($serverOrMessage)."\t";
    	    $ErrorCount = 0;
    	}
    	elsif (substr($Error, 0, 5) eq "Error")
            {
    	    if ($ErrorCount > 0)
    	    {
                	print outputFile ("\t\t".$serverOrMessage)."\n";
    	    }
    	    else
    	    {
    	     	print outputFile ("\t".$serverOrMessage)."\n";
    		$ErrorCount++;
    	    }
            }
        }
    }
    close(inputFile);
    close(outputFile);
    Enjoy! =)


    Originally posted by josephg
    How can I transfer this data file using activeperl in to column format as bellow output file

    Data file contain
    Server alfa1
    Error system down

    Server alfa2
    Error network proble
    Error disk problem
    Error key board problem

    Server alfa3
    Error system down
    Error network problem

    The output file

    Server Error
    Alfa1 system down

    Alfa2 network proble
    disk problem
    key board problem

    alfa3 system down
    network problem

  3. #3
    Join Date
    Sep 2003
    Posts
    31
    Yes I have tried your codes, there is no out put,
    the input file, between server and server name and error and error message is tab'ed

    I have attached the script input and output file,

    Server Error

    Alfa1 system down

    Alfa2 network proble
    disk problem
    key board problem

    alfa 3 system down
    network problem
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    I would do it like this:
    Code:
    $/ = "\r?\n\r?\n";
    open(R_INPUT,"<$input_file") or die "Error (Input): $!";
    my @servers = <R_INPUT>;
    close R_INPUT;
    
    open(W_OUTPUT,">$output_file") or die "Error (Output): $!");
    print W_OUTPUT "Server Error\n\n";
    foreach my $server(@servers){
      my @servermessages = split(/\n/,$server);
      foreach $message(@servermessages){
        if($message =~ /^Server/){
          $message =~ s/^Server\s//;
          print W_OUTPUT $message;
        }
        elsif($message =~ /^Error\s/){
          $message =~ s/^Error\s//;
          print W_OUTPUT $message,"\n";
      }
      print "\n";
    }
    close W_OUTPUT;
    Last edited by reneeb; 02-04-04 at 15:32.

  5. #5
    Join Date
    Nov 2003
    Posts
    65

    Cool

    Hey your input file was not tab dilimeted. Unless your tab is set to one space then its acting as space not a tab. But here im sending u back the tab input file and the same code and take a look at the outfile it produces.
    Hope this helps.

    Originally posted by josephg
    Yes I have tried your codes, there is no out put,
    the input file, between server and server name and error and error message is tab'ed

    I have attached the script input and output file,

    Server Error

    Alfa1 system down

    Alfa2 network proble
    disk problem
    key board problem

    alfa 3 system down
    network problem
    Attached Files Attached Files

  6. #6
    Join Date
    Sep 2003
    Posts
    31
    llccoo

    That worked but the output file are not separted by new line,How do we do this?? (see REQUIRED OUTPUT)
    current output:
    Server Error
    alfa1 system down
    alfa2 network proble
    disk problem
    key board problem
    alfa3 system down
    network problem

    REQIRED OUTPUT

    Server Error
    alfa1 system down

    alfa2 network proble
    disk problem
    key board problem

    alfa3 system down
    network problem


    Thanx reneeb
    I tried your script but the output did not worked as required

  7. #7
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Oh I see, there's a mistake in my code: change $/ = "\r?\n\r?\n" to
    $/ = "\n\n";
    Last edited by reneeb; 02-06-04 at 05:10.
    board.perl-community.de - The German Perl-Community

  8. #8
    Join Date
    Nov 2003
    Posts
    65

    Cool

    oh ok thats not hard just gotta add a carriage return.
    Look for this part in the code i gave you:
    Code:
            if (substr($Error, 0, 6) eq "Server")
            {
                print outputFile ($serverOrMessage)."\t";
    	    $ErrorCount = 0;
    	}
    	elsif (substr($Error, 0, 5) eq "Error")
            {
    and add to the print line command "\n" to the beginning of the print statement so now the code should look like this:
    Code:
            if (substr($Error, 0, 6) eq "Server")
            {
                print outputFile "\n".($serverOrMessage)."\t";
    	    $ErrorCount = 0;
    	}
    	elsif (substr($Error, 0, 5) eq "Error")
            {
    hope this works out for you.
    Enjoy!

    Originally posted by josephg
    llccoo

    That worked but the output file are not separted by new line,How do we do this?? (see REQUIRED OUTPUT)
    current output:
    Server Error
    alfa1 system down
    alfa2 network proble
    disk problem
    key board problem
    alfa3 system down
    network problem

    REQIRED OUTPUT

    Server Error
    alfa1 system down

    alfa2 network proble
    disk problem
    key board problem

    alfa3 system down
    network problem


    Thanx reneeb
    I tried your script but the output did not worked as required

  9. #9
    Join Date
    Sep 2003
    Posts
    31

    Talking transfer this data file using activeperl in to column format

    Thanx all ok now

  10. #10
    Join Date
    Nov 2003
    Posts
    65

    Cool Re: transfer this data file using activeperl in to column format

    no problem =)

    Originally posted by josephg
    Thanx. all ok now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •