I am trying to create a script that searches certain fields from the output of autorep -j $i -q
and print out what is value of that field mentioned in the output.
So basically autorep -j $i -q
, when the script is executed will ask the user to input a JOBNAME
or %SEARCHSTRING%
and then it will give the job details in the below format:
/tmp $ autorep -j Test_jobA -q
insert_job: Test_jobA job_type: CMD
command: echo
machine: machinename
owner: owner
permission:
date_conditions: 1
run_calendar: Autosys_Calendar
start_times: "09:09"
description: "test discription"
std_out_file: "/tmp/Test_jobA.out"
std_err_file: "/tmp/Test_jobA.err"
alarm_if_fail: 1
alarm_if_terminated: 1
insert_job: Test_JobB job_type: CMD
command: echo
machine: machinename
owner: owner
permission:
date_conditions: 1
days_of_week: mo,tu,we,th,fr
start_times: "21:05"
description: "test discription"
std_out_file: "/tmp/Test_JobB.out"
std_err_file: "/tmp/Test_JobB.err"
alarm_if_fail: 1
alarm_if_terminated: 1
insert_job: Test_JobC job_type: BOX
command: echo
machine: machinename
owner: owner
permission:
date_conditions: 0
description: "test discription"
std_out_file: "/tmp/Test_JobC.out"
std_err_file: "/tmp/Test_JobC.err"
alarm_if_fail: 1
alarm_if_terminated: 1
insert_job: Test_JobD job_type: CMD
command: echo
machine: machinename
owner: owner
permission:
date_conditions: 1
days_of_week: su
start_times: "08:50"
description: "test discription"
std_out_file: "/tmp/Test_JobD.out"
std_err_file: "/tmp/Test_JobD.err"
alarm_if_fail: 1
alarm_if_terminated: 1
insert_job: Test_JobE job_type: CMD
command: echo
machine: machinename
owner: owner
permission:
date_conditions: 1
days_of_week: su
start_times: "08:20"
description: "test discription"
std_out_file: "/tmp/Test_JobE.out"
std_err_file: "/tmp/Test_JobE.err"
alarm_if_fail: 1
alarm_if_terminated: 1
insert_job: Test_JobF job_type: CMD
command: echo
machine: machinename
owner: owner
permission:
date_conditions: 1
days_of_week: all
start_mins: 0,10,20,30,40,50
description: "test discription"
std_out_file: "/tmp/Test_JobF.out"
std_err_file: "/tmp/Test_JobF.err"
alarm_if_fail: 1
alarm_if_terminated: 1
So as you can see if date_condition: 0
then the job may or may not have condition:
in it and wont have days_of_week: start_mins: run_window: run_calendar:
and if a job has date_condition: 1
then it may or may not have days_of_week: start_mins: run_window: run_calendar:
I have the below script that does the filtering well enough:
#!/bin/bash
TXT=/tmp/test1.txt
CSV=/tmp/test1.csv
echo "Enter the JOB_NAME or %SEARCHSTRING%"
while read -r i;
do
awk '
/^insert_job/ {if (flag) {printf "\n"};
printf "%s %s ", $2, $4;
flag = 1};
/^date_conditions/ {printf "%s", $2};
/^condition:|^days_of_week:|^run_calendar:|^start_times:|^start_mins:/ {printf "%s", $2}
' < <(autorep -j $i -q) > $TXT
break
done
if [ -s $TXT ]
then
(echo "job_name,job_type,Date_Conditions,condition,days_of_week,start_times,Start_mins" ; cat test1.txt) | sed 's/ \+/,/g' > $CSV
else
echo "Please check the %SEARCHSTRING% or JOB_NAME"
fi
The While
loop in the above script gives me the below output:
Test_jobA CMD 1 Autosys_Calendar "09:09"
Test_JobB CMD 1 mo,tu,we,th,fr "21:05"
Test_Jobc BOX 0
Test_JobD CMD 1 su "08:50"
Test_JobE CMD 1 su "08:20"
Test_JobF CMD 1 all "02:02,04:04,06:06,08:08,10:10,12:12,14:14,16:16,18:18,20:20,22:22"
The IF condition
in the above script is used for converting the output of While loop
into a .csv
file but as the output is not being lenear, i am getting wrong data in the wrong columns.
is there anyway i can smooth it out.
EDIT: The required output for the CSV file:
I am looking for the below output, if a field is missing from the job then instead of an empty field it should print "NA" instead so that the .csv formating could be in line
job_name job_type date_conditions condition run_calendar days_of_week start_times start_mins
Test_jobA CMD 1 NA Autosys_Calendar NA "09:09" NA
Test_JobB CMD 1 NA NA mo,tu,we,th,fr "21:05" NA
Test_Jobc BOX 0 NA NA NA NA NA
Test_JobD CMD 1 NA NA su "08:50" NA
Test_JobE CMD 1 NA NA su "08:20" NA
Test_JobF CMD 1 NA NA all NA 0,10,20,30,40,50