To get the job status from second line

I want to get the job status from the second line "SU" to a variable

Input:

Job Name   Last Start                 Last End      ST  Run/Ntry   Pri/Xit 
JOBNAME   10/24/2016 10:34:55  10/24/2016 10:44:15  SU  100344/32  0

Code tried

JSTATUS=$(cat Input | awk '$6 == "SU" {print $6}')

Desired output:

echo $JSTATUS 
SU

But when job is running, "Last End" goes empty and I cant search with $6, is there a way to search first row "ST" and then get the respective below status?

Hello Joselouis,

Could you please try following and let me know if this helps you.

JSTATUS=$(awk 'NR==2 && NF==8{print $6}'  Input_file)
echo $JSTATUS

Above code will check each time line number 2nd and then will check if number of fields are 8, if this condition is satisfied then only it will print 6th field as per your request, could you please try above and let me know how it goes then.

EDIT: I saw you have edited your post and added condition of checking first line of looking for string ST and look for the same field on 2nd line, this could be done but in your shown sample Input_file seems value of string ST is coming on a lesser field than what it is on first line if this is a typo then following may hep you in same.

awk 'NR==1{for(i=1;i<=NF;i++){if($i=="ST"){Q=i}};next} {print $Q}'  Input_file

If your sample Input_file is NOT having typo and each time value of string ST will be in 1 field lessser than 1st line then you could tweak the code as follows too.

awk 'NR==1{for(i=1;i<=NF;i++){if($i=="ST"){Q=i}};next} {print $(Q-1)}'   Input_file

NOTE: Considering our Input_file will have only 2 lines.

Thanks,
R. Singh

How about using the third last field

JSTATUS=$(awk 'NR==2{print $(NF-2)}' Input)

or the two characters at position 53-54:

JSTATUS=$(awk 'NR==2{print substr($0,53,2)}' Input)

Here is another one:

awk 'NR==1 {a=index($0,"ST")} NR>1 { print substr($0,a,2)} ' file

Assuming line contains JOBNAME has two characters is what you want .

if the autosys job is in RU/ST status, then we will only have 6 fields. so we have to get the status of the job like this..

awk 'NR>1&&NF==6{print $(NF-2)}NR>1&&NF==8{print $(NF-3)}' Input

Thanks a lot for all your input, everything really helped

And also I got another autosys way to do it

autostatus -j JOBNAME 

Above command gave me the status.. thank you all