使用perl

时间:2017-10-20 01:49:09

标签: perl autosys jil

我有一个解析AutoSys JIL文件的任务。这是一个JIL作业定义,它是AUTOSYS调度程序读入并运行的配置文件。 ,想象一下像这样格式化的文件,其中有数千个作业定义,如下所示,以完全相同的格式堆叠在一起。全部以标题开头,以时区结束。

/* ----------------- COME_AND_PLAY_WITH_US_DANNY ----------------- */

insert_job: COME_AND_PLAY_WITH_US_DANNY   job_type: CMD
command: /bin/bash -ls
machine: capser.com
owner: twins
permission: foo,foo
date_conditions: 1
days_of_week: mo,tu,we,th,fr
start_times: "04:00"
description: "Forever, and ever and ever"
std_in_file: "/home/room217"
std_out_file: "${CASPERSYSLOG}/room217.out"
std_err_file: "${CASPERSYSLOG}/room217.err
alarm_if_fail: 1
profile: "/autosys_profile"
timezone: US/Eastern

这是脚本。我需要从上面的作业定义中提取作业,机器和命令。它工作正常,但最终我想要将信息存储在某种容器中并发送它,而这个脚本在终端中逐行写出结果。现在我正在将结果重定向到一个临时文件。

#!/foo/bar/perl5/core/5.10/exec/bin/perl
use strict;
use warnings;
use File::Basename ;

my($job, $machine, $command)  ;
my $filename = '/tmp/autosys.jil_output.padc';
open(my $fh, '<:encoding(UTF-8)', $filename)
  or die "Could not open file '$filename' $!";
my $count = 0;
while (my $line = <$fh>) {
    #chomp $line;
    if($line =~ /\/\* -{17} \w+ -{17} \*\//) {
    $count = 1; }
    elsif($line =~  /(alarm_if_fail:)/) {
    $count = 0 ; }
    elsif ($count) {
             if ($line =~ m/insert_job: (\w+).*job_type: CMD/) {
             $job = $1   ;
             }
             elsif($line =~ m/command:(.*)/) {
             $command = $1  ;
             }
             elsif($line =~ m/machine:(.*)/) {
             $machine = $1  ;

             print "$job\t $machine\t $command \n ";      
             }
        }


    #sleep 1 ;
   }

我的问题是当我在最后一个elsif语句中放置print $ job,$ machine $ command 语句时,它运行正常。但是当我把它放在最后一个elsif语句的一边时,就像下面的例子一样,输出会一遍又一遍地重复 - 每行在输出中重复四到五次。我不明白这个。为什么我必须将print语句放在最后一个elsif语句中,以使脚本一次打印出一行,正确。

elsif ( $line =~ m/machine:(.*)/ ) {
    $machine = $1;
}

print "$job\t $machine\t $command \n ";


重新格式化上述代码以提高可读性

#!/foo/bar/perl5/core/5.10/exec/bin/perl

use strict;
use warnings;

use File::Basename;

my ( $job, $machine, $command );
my $filename = '/tmp/autosys.jil_output.padc';

open( my $fh, '<:encoding(UTF-8)', $filename )
        or die "Could not open file '$filename' $!";

my $count = 0;

while ( my $line = <$fh> ) {

    #chomp $line;
    if ( $line =~ /\/\* -{17} \w+ -{17} \*\// ) {
        $count = 1;
    }
    elsif ( $line =~ /(alarm_if_fail:)/ ) {
        $count = 0;
    }
    elsif ( $count ) {

        if ( $line =~ m/insert_job: (\w+).*job_type: CMD/ ) {
            $job = $1;
        }
        elsif ( $line =~ m/command:(.*)/ ) {
            $command = $1;
        }
        elsif ( $line =~ m/machine:(.*)/ ) {
            $machine = $1;
            print "$job\t $machine\t $command \n ";
        }
    }

    # sleep 1;
}

2 个答案:

答案 0 :(得分:2)

正如我在评论中所说,请理智地格式化您的代码。如果不这样做,你会让人们无视你的问题,或者像我这样回答那些脾气暴躁的人

  • 我们假设未识别的文本块只是您输入的一个示例

  • 我们还假设,即使您的代码与您的示例数据一起正常工作,实际数据中仍有一些数据块不起作用

  • 最重要的是,我假设包含空格的任何数据字段值都需要包含引号,这会使您的示例command: /bin/bash -ls不正确,语法无效

还请确保您提供了有关可运行代码和数据问题的正确示例。如果我执行您针对示例数据显示的代码,那么一切正常,那么您有什么问题?

据我所知,您希望显示insert_job字段为{{1}的每个JIL数据块中的machinecommandjob_type字段}}。是吗?

这是我最好的猜测:xxfelixxx's comment是正确的,您只是打印每次从数据文件中读取一行时收集的所有字段

我的解决方案是将每个数据块转换为哈希值。

使用注释来描述块是危险的,并且您没有提供有关字段排序的信息,因此我必须假设CMD字段首先出现。如果要将文件用作命令列表,那么这是有道理的,但同一行上的额外insert_job字段很奇怪。这是您的数据的真实样本,还是您的示例的其他问题?

这是我对你的问题想象力的有效解决方案。

job_type

输出

#!/foo/bar/perl5/core/5.10/exec/bin/perl

use strict;
use warnings 'all';

my $data = do {
    local $/;
    <DATA>;
};

my @data = grep /:/, split /^(?=insert_job)/m, $data;

for ( @data ) {

    my %data = /(\w+) \s* : \s* (?| " ( [^""]+ ) " | (\S+) )/gx;

    next unless $data{job_type} eq 'CMD';

    print "@data{qw/ insert_job machine command /}\n";
}


__DATA__
/* ----------------- COME_AND_PLAY_WITH_US_DANNY ----------------- */

insert_job: COME_AND_PLAY_WITH_US_DANNY   job_type: CMD
command: /bin/bash -ls
machine: capser.com
owner: twins
permission: foo,foo
date_conditions: 1
days_of_week: mo,tu,we,th,fr
start_times: "04:00"
description: "Forever, and ever and ever"
std_in_file: "/home/room217"
std_out_file: "${CASPERSYSLOG}/room217.out"
std_err_file: "${CASPERSYSLOG}/room217.err
alarm_if_fail: 1
profile: "/autosys_profile"
timezone: US/Eastern

/* ----------------- COME_AND_PLAY_WITH_US_AGAIN_DANNY ----------------- */

insert_job: COME_AND_PLAY_WITH_US_AGAIN_DANNY   job_type: CMD
command: /bin/bash -ls
machine: capser.com
owner: twins
permission: foo,foo
date_conditions: 1
days_of_week: mo,tu,we,th,fr
start_times: "04:00"
description: "Forever, and ever and ever"
std_in_file: "/home/room217"
std_out_file: "${CASPERSYSLOG}/room217.out"
std_err_file: "${CASPERSYSLOG}/room217.err
alarm_if_fail: 1
profile: "/autosys_profile"
timezone: US/Eastern

/* ----------------- NEVER_PLAY_WITH_US_AGAIN_DANNY ----------------- */

insert_job: NEVER_PLAY_WITH_US_AGAIN_DANNY   job_type: CMD
command: /bin/bash -rm *
machine: capser.com
owner: twins
permission: foo,foo
date_conditions: 1
days_of_week: mo,tu,we,th,fr
start_times: "04:00"
description: "Forever, and ever and ever"
std_in_file: "/home/room217"
std_out_file: "${CASPERSYSLOG}/room217.out"
std_err_file: "${CASPERSYSLOG}/room217.err
alarm_if_fail: 1
profile: "/autosys_profile"
timezone: US/Eastern

答案 1 :(得分:0)

这是一个ksh解决方案,可将JIL文件转换为可在excel中打开的逗号分隔文件

#!/usr/bin/ksh

# unix scprit to flatten autorep -q

resetVar()
{
    AIF=""
    AD=""
    AH=""
    BF=""
    BN=""
    BS=""
    BT=""
    COM=""
    COD=""
    DC=""
    DOW=""
    DES=""
    EC=""
    IJ=""
    JL=""
    JT=""
    MAC=""
    MES=""
    MRA=""
    NR=""
    OWN=""
    PER=""
    PRI=""
    PRO=""
    RC=""
    RW=""
    SM=""
    ST=""
    SEF=""
    SOF=""
    TRT=""
    WF=""
    WFMS=""
    WI=""
    LSD=""
    LST=""
    LED=""
    LET=""
    STA=""
    RUN=""
}


writePartToFile()
{
 echo "$AIF;$AD;$AH;$BF;$BN;$BS;$BT;$COM;$COD;$DC;$DOW;$DES;$EC;$IJ;$JL;$JT;$MAC;$MES;$MRA;$NR;$OWN;$PER;$PRI;$PRO;$RC;$RW;$SM;$ST;$SEF;$SOF;$TRT;$WF;$WFMS;$WI" >> $TO_TPM
 #echo "$AIF;$AD;$AH;$BF;$BN;$BS;$BT;$COM;$COD;$DC;$DOW;$DES;$EC;$IJ;$JL;$JT;$MAC;$MES;$MRA;$NR;$OWN;$PER;$PRI;$PRO;$RC;$RW;$SM;$ST;$SEF;$SOF;$TRT;$WF;$WFMS;$WI" 
 resetVar

}

JOB_NAME="flatten JIL"
part1=""
part2=""


#---------------------------------
if test "$1." = "."
then
   echo "Missing first parameter (jil file to flatten)"; 
   exit 1;
fi

if test "$2." = "."
then
   echo "Missing second parameter (resulting flat file)";
 exit 1;
fi

TO_FLATTEN=$1
TO_RESULT=$2
CLE_FILE="lesCles"
CLE_TMP="lesClesTmp"
TO_TPM="tempFichier"
TO_STATUS="statusFichier"

rm $TO_RESULT
rm $CLE_TMP
rm $CLE_FILE
rm $TO_TPM
rm $TO_STATUS

echo 'alarm_if_fail;auto_delete;auto_hold;box_failure;box_name;box_success;box_terminator;command;condition;date_conditions;days_of_week;description;exclude_calendar;insert_job;job_load;job_terminator;machine;max_exit_success;max_run_alarm;n_retrys;owner;permission;priority;profile;run_calendar;run_window;start_mins;start_times;std_err_file;std_out_file;term_run_time;watch_file;watch_file_min_size;watch_interval;last_start_date;last_start_time;last_end_date;last_end_time;status;run' >> $TO_RESULT;
 while read line; do    
    if test "${line#*:}" != "$line"
    then        
      cle="$(echo "$line" | cut -d":" -f 1)"
      #echo "cle = $cle"
      part2="$(echo "$line" | cut -d":" -f 2)"   
      #echo "part2 = $part2"        
      val="$(echo "$part2" | cut -d" " -f 2)"
      #echo "val = $val"    
    fi  
    if test "$cle" = "insert_job"
    then
    #on n'est sur la premiere ligne
        if test "$IJ." = "."
        then
            ;                           
        else          
            if test "$BN." = "."
            then             
             echo $IJ >> $CLE_TMP
            else
             echo $BN >> $CLE_TMP
            fi      
            writePartToFile         
        fi
        IJ=$val
        JT="$(echo "$line" | cut -d":" -f 3)"                   
    else    
    #on n est pas sur le premiere ligne 
        val=$part2
        case $cle in
            alarm_if_fail) AIF=$val;;
            auto_delete) AD=$val;;
            auto_hold) AH=$val;;
            box_failure) BF=$val;;
            box_name) BN=$val;;
            box_success) BS=$val;;
            box_terminator) BT=$val;;
            command) COM=$val;;
            condition) COD=$val;;
            date_conditions) DC=$val;;
            days_of_week) DOW=$val;;
            description) DES=$val;;
            exclude_calendar) EC=$val;;
            insert_job) IJ=$val;;
            job_load) JL=$val;;
            job_terminator) JT=$val;;
            machine) MAC=$val;;
            max_exit_success) MES=$val;;
            max_run_alarm) MRA==$val;;
            n_retrys) NR=$val;;
            '#owner') OWN=$val;;
            permission) PER=$val;;
            priority) PRI=$val;;
            profile) PRO=$val;;
            run_calendar) RC=$val;;
            run_window) RW=$val;;
            start_mins) SM=$val;;
            start_times) ST=$val;;
            std_err_file) SEF=$val;;
            std_out_file) SOF=$val;;
            term_run_time) TRT=$val;;
            watch_file) WF=$val;;
            watch_file_min_size) WFMS=$val;;
            watch_interval) WI=$val;; 
        esac        
    fi

done  < $TO_FLATTEN;
#Traiter derniere occurence
if test "$BN." = "."
then
    echo $IJ >> $CLE_TMP
else
    echo $BN >> $CLE_TMP
fi      
writePartToFile     

echo "Les cles"
cat $CLE_TMP | sort | uniq > $CLE_FILE
cat $CLE_FILE
rm $CLE_TMP

#------------------------------
 while read line; do        
    autorep -J ${line} -w  >> $TO_STATUS;   
done  < $CLE_FILE;
#----------------------------------------
echo " Resultats"
while read line; do
unJob="$(echo "$line" | cut -d";" -f 14)"
details="$(grep -w  "$unJob" "$TO_STATUS" | head -n 1)" 
LSD="$(echo "$details" | awk '{print $2}')"
if test "$LSD" = "-----"
then
    LST=""
    LED="$(echo "$details" | awk '{print $3}')"
    if test "$LED" = "-----"
    then
        LET=""
        STA="$(echo "$details" | awk '{print $4}')"
        RUN="$(echo "$details" | awk '{print $5}')"
    else
        LET="$(echo "$details" | awk '{print $4}')"
        STA="$(echo "$details" | awk '{print $5}')"
        RUN="$(echo "$details" | awk '{print $6}')"
    fi
else
    LST="$(echo "$details" | awk '{print $3}')"
    LED="$(echo "$details" | awk '{print $4}')"
    if test "$LED" = "-----"
    then
        LET=""
        STA="$(echo "$details" | awk '{print $5}')"
        RUN="$(echo "$details" | awk '{print $6}')"
    else
        LET="$(echo "$details" | awk '{print $5}')"
        STA="$(echo "$details" | awk '{print $6}')"
        RUN="$(echo "$details" | awk '{print $7}')"
    fi
fi

echo " ligne= ${line};${LSD};${LST};${LED};${LET};${STA};${RUN}"
echo "${line};${LSD};${LST};${LED};${LET};${STA};${RUN}" >> $TO_RESULT
resetVar
done  < $TO_TPM;