chidori
December 3, 2013, 10:51am
1
Hi ,
I have a jil file which i am trying to parse and print the job name and the condition corresponding to it.
Below is the input file
/* -------------------- testjob1 -------------------- */
insert_job: testjob1 job_type: c
machine: unix
owner: chidori
condition: s(joba) and s(jobc)
std_out_file: /export/home/chidori/test.out
std_err_file: /export/home/chidori/test.err
/* -------------------- testjob2 -------------------- */
insert_job: testjob2 job_type: c
machine: unix
owner: chidori
condition: s(jobc)
std_out_file: /export/home/chidori/test.out
std_err_file: /export/home/chidori/test.err
/* -------------------- testjob3 -------------------- */
insert_job: testjob3 job_type: c
machine: unix
owner: chidori
condition: s(joba) or s(jobc)
std_out_file: /export/home/chidori/test.out
std_err_file: /export/home/chidori/test.err
I have figured out this one liner to do this
awk '{sub(/^ */,"");if($0~/^$){a++};if($0~/^\//){printf "%s = ", $3};if($0~/^condition:/){sub("condition: ","");print}}' sample.jil
Condition:
At times a job might not have any condition in them , in that case the script should print as "No_Condition".
I am not able to figure out how to write the code that can check if condition string does not exist then print "No_Condition". Can someone please help me out.
Also i was thinking if we can consider from the line that begins with /*
to the line before the next /*
as one record and do some processing. But was not sure how to implement the same.
Yoda
December 3, 2013, 11:09am
2
Here is one way of doing it:
awk '
/^[ ]*\/\*/ {
if ( NR > 1 && !(f) )
print "No_Condition"
f = 0
printf ( "%s = ", $3 )
}
/condition:/ {
f = 1
sub ( /[ ]*condition:[ ]*/, X)
print
}
END {
if (!(f))
print "No_Condition"
}
' sample.jil
1 Like
chidori
December 3, 2013, 11:18am
3
Can you please explain the logic around here
RudiC
December 3, 2013, 11:20am
4
This is a starting point and needs some refinement, but it can guide you to a better solution:
awk 'NF==1 {next} {for (i=1; i<=NF; i++) if ($i ~ /insert|condition| or|and/) print $(i+1)} !/condition/ {print "no_condition"}' RS=-+ file
testjob1
s(joba)
s(jobc)
testjob2
s(jobc)
testjob3
no_condition
(I removed the condition line from the third record)
Yoda
December 3, 2013, 11:22am
5
If am using a flag variable: f
and it is set when awk finds pattern /condition:/
So when awk finds next pattern /^[ ]*\/\*/
for job name, I am checking if this flag variable is set or not.
If not set, then print No_Condition
. Same is done in the END rule to cover last job name.
chidori
December 3, 2013, 11:28am
6
Thanks, Whats up with NR > 1
why we are going for a record greater than 1
---------- Post updated at 12:28 PM ---------- Previous update was at 12:26 PM ----------
rudic:
This is a starting point and needs some refinement, but it can guide you to a better solution:
awk 'NF==1 {next} {for (i=1; i<=NF; i++) if ($i ~ /insert|condition| or|and/) print $(i+1)} !/condition/ {print "no_condition"}' RS=-+ file
testjob1
s(joba)
s(jobc)
testjob2
s(jobc)
testjob3
no_condition
(I removed the condition line from the third record)
Hey RudiC, Can you please explain the record seperator we use here. RS=-+
Yoda
December 3, 2013, 11:29am
7
I used NR > 1
because for first job name, the flag variable: f
is going to be initially 0 or null anyway.
So I don't want to check if flag is set or not for first job, but for all subsequent jobs.
RudiC
December 3, 2013, 11:36am
8
RS="-+"
matches the strings of minuses that indeed separate the records. Should there be other minus signs in records, the RS would nee to be adapted.
Like this ?
$ awk '/insert_job/{printf "%s = ",$2}/condition/{gsub(/.*: /,x);printf "%s\n", $0 ? $0 :"No Condition"}' file
testjob1 = s(joba) and s(jobc)
testjob2 = s(jobc)
testjob3 = s(joba) or s(jobc)
chidori
December 3, 2013, 12:15pm
10
Like this ?
$ awk '/insert_job/{printf "%s = ",$2}/condition/{gsub(/.*: /,x);printf "%s\n", $0 ? $0 :"No Condition"}' file
testjob1 = s(joba) and s(jobc)
testjob2 = s(jobc)
testjob3 = s(joba) or s(jobc)
Sorry, that doesn't seem to work when there no "condition" tag
Something like this
/* -------------------- testjob1 -------------------- */
insert_job: testjob1 job_type: c
machine: unix
owner: chidori
std_out_file: /export/home/chidori/test.out
std_err_file: /export/home/chidori/test.err