LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-20-2014, 04:28 AM   #1
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Rep: Reputation: 64
Perl script, output format wrong


I have this perl script which should be trimming the content of a file and showing output as below format.

MSDCMGMGT_SC- MSDC-HIPS-01; ;08/13/2012 12:00:00;Missed
MSDCMGMGT_SC- MSDC-AV-01; ;08/13/2012 12:00:00;Missed
MSDCMGMGT_SC- MSDC-HIPS-01; ;08/13/2012 12:00:00;Missed
MSDCMGMGT_SC- MSDC-AV-01; ;08/13/2012 12:00:00;Fail

But I am getting wrong output for the line with 'Failed' (first line) as below although Missed ones are showing okay,

08/13/2012 11:24:29; ;08/13/2012 11:00:00;MSDCTNM_SCHED
MSDCMGMGT_SC- MSDC-HIPS-01; ;08/13/2012 12:00:00;Missed
MSDCMGMGT_SC- MSDC-AV-01; ;08/13/2012 12:00:00;Missed
MSDCMGMGT_SC- MSDC-HIPS-01; ;08/13/2012 12:00:00;Missed
MSDCMGMGT_SC- MSDC-AV-01; ;08/13/2012 12:00:00;Missed

I have attached both the script and the file, could anyone please help?
Attached Files
File Type: txt BACKUP.txt (3.2 KB, 13 views)
File Type: txt abcscript.txt (1.1 KB, 16 views)
 
Old 11-20-2014, 05:28 AM   #2
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Original Poster
Rep: Reputation: 64
Okay, I found why for some rows it is showing the wrong format. Wherever there is data in the Actual start column, the output is coming wrong.
But how to ignore that?
 
Old 11-20-2014, 10:16 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
SO I am not a perl guru, but I do have some pointers

1. Please use strict; at the top of your script and you will uncover many errors

2. As it stands, even without the above I get warnings

3. You have seemed to have over complicated the script quite a bit (more on this in a minute)

4. Your variable names are very misleading based on what they have stored in them:
Code:
08/13/2012   12:00:00  MSDCMGMGT_SC- MSDC-HIPS-01 Missed
$backup_name $nodename $date1        $date2       $type
To me, only $type makes any sense

5. The reason for your unusual output, apart from the ordering above, is that a Failed entry has 2 additional fields
Code:
08/13/2012 11:00:00      08/13/2012 11:24:29      MSDCTNM_SCHED     MSDC-TIVOLI--     Failed
08/13/2012 12:00:00                               MSDCMGMGT_SC-     MSDC-HIPS-01      Missed
So with a little googling / perldocing I found you could probably go with:
Code:
#!/usr/bin/perl -w

use strict;

open(DATA, "</root/Desktop/BACKUP.TXT") or die "Couldn't open file";

while (<DATA>)
{
  chomp;

  if( m/(Missed|Failed)\s*$/ )
  {
    my @fields = split;
  
    if( scalar @fields == 5 )
    {
      print "$fields[2] $fields[3];;$fields[0] $fields[1];$fields[4]\n";
    }   
    else
    {   
      print "$fields[4] $fields[5];;$fields[0] $fields[1];$fields[6]\n";
    }   
  }
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell script output in .csv format mangal023 Linux - Newbie 3 02-24-2014 09:54 PM
Print output of a script to screen using Perl/Multiple installation of Perl Modules metallica1973 Linux - General 1 02-17-2011 05:59 PM
Help needed output format in perl script priyophan Linux - Software 6 01-29-2011 04:52 PM
perl script to parse following format suomali Programming 11 09-24-2008 01:51 PM
What's wrong with this perl script ohcarol Programming 6 11-01-2006 08:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration