Operating System - HP-UX
1825795 Members
2211 Online
109687 Solutions
New Discussion

Re: Get a particular column value

 
intp
Frequent Advisor

Get a particular column value

Hi,

How to get a particular column value from a
file or from output of another command.
See attached sample file.

We use autosys(scheduler program) for scheduling jobs (calls shell scripts).

we issue a command in unix to see status of a autosys job like ,

autorep

and it returns the info as in the attached file.

My Requirement is to ... check this file
and see whether there are any failures ..
which i can get by manually reading the values in the 4th column (ST - status)...it would say SU for success , FA for failure , RU for running etc...

Now i CANT just scan for 'FA' cos some jobnames also has 'FA' in them. I want to know how to get the values of 4th column alone and check for 'FA' in them and report back with
jobname and status.

Points to note.

1) lines are not alligned properly (not sure whthr this matters)

2) in header 4th column is ST but from next line 4th column is date value.

may be a wild thought ..how to read a line from backwards (so that status column is always 3rd)

HELP.
9 REPLIES 9
Anil C. Sedha
Trusted Contributor

Re: Get a particular column value

You need to run the following either on the command menu or write a script to do this


autorep | awk '{print $4}' | grep FA

(the print variable 4 is meant for the fourth column. you can tweak its value to see if you are getting the correct column. Then when grep searches for FA it will show it to you on the command line).

If you need to learn, now is the best opportunity
A. Clay Stephenson
Acclaimed Contributor

Re: Get a particular column value

Well, here is how to extract the 3rd from last field in a line:

awk '{ if (NF > 2) print $(NF - 2) }' < infile

If you want to capture that in a shell variable then:

typeset DT=""
awk '{ if (NF > 2) print $(NF - 2) }' < infile | while read DT
do
echo "Date = ${DT}"
done
If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: Get a particular column value

It looks like you want the status from the 3rd line and that that status field looks like it is in the 6th field (space delimited), there fore this should work-

statuscode=`autorep job | awk 'NR=3{print $6}'`

Then you can check statuscode.

if [[ $statuscode = "SU" ]] ; then ...

HTH

-- Rod Hills
There be dragons...
intp
Frequent Advisor

Re: Get a particular column value

ROD

statuscode=`autorep job | awk 'NR=3{print $6}'`
if you refer to previously attached sample file...your code prints properly for
lines 3 to 6....line 7 & 9 prints blanks and 8 & 10 prints last column. so it changes dynamically.

AC CLAY

awk '{ if (NF > 2) print $(NF - 2) }' < infile
I'm sorry , its not always going to be 3rd column from last.. if you refer to
previously attached sample file.."SU" (in data records) is in 2nd column from last.
but when there is some value in RUN column then thats going to move to 3rd column.
so it changes dynamically.
Hein van den Heuvel
Honored Contributor

Re: Get a particular column value

Why not simple grep/awk/perl for " FA "
it seems there are no spaces, and certainly not " FA " to be expected in the jobname.

Given your specification so far Clay's solution should work, it looks backwards using awk's NF (number of fields) as the variable.

You coudl make any solution more robust by using the date/time as search anchor:
/\s+\d+:\d+\s+FA\s+/

Using perl or awk you can also readily remember the job name if that was printed on a seperate line. This technique can also be used to make the status column be on a fixed location:

# perl -ne 'chomp; $_ = $prior . $_; $prior = /\d$/? "" : $_; print "$_\n" if / FA /' your-file

enjoy,
Hein.


Hein van den Heuvel
Honored Contributor

Re: Get a particular column value


Here is an other approach for a shell/awk/perl script.

Read the file.
If you do not see a ":" on the line, then assume it is just the job name.
In that case, just explictly read a next line and add it on.

#perl -ne "$_ .=<> unless /:/; @words=split; print \"$words[@words-2] $words[0]\n\" if /:/" x.tmp
SU TEST_GHT_INPUT_TABLES_B
SU TEST_PRS_acctUCT_UNIT_B
SU TEST_fw_GHT_acctuctunit_f
FA TEST_sh_GHT_facctuctmove_c
FA TEST_sh_trunc_GHT_acctunit_c
RU TEST_sh_load_GHT_FAunit_c

In perl you could also 'unshift' a saved jobname into a short line.

awk example:

$ awk '(NF<2) {x=$1;getline; $0 =x $0} /:/ && (NF > 2){ print $1, $6}' your-file
TEST_GHT_INPUT_TABLES_B SU
TEST_PRS_acctUCT_UNIT_B SU
TEST_fw_GHT_acctuctunit_f SU
TEST_sh_GHT_facctuctmove_c FA
TEST_sh_trunc_GHT_acctunit_c FA
TEST_sh_load_GHT_FAunit_c RU


Cheers,
Hein.
James R. Ferguson
Acclaimed Contributor

Re: Get a particular column value

Hi:

If you like the 'awk' approach, you can reference any column (field) from the last in a line (row) thusly:

# echo "a b c d e f"|awk '{if ($(NF-2)~/d/) {print $(NF-2)}}'

In the above example, I want a field that matches "d" only when it is 2-fields removed from the end (NF) of the line.

In 'awk' NF is the number of fields in the last line read.

The assumption is that the field delimiter is whitespace (spaces, tabs). If it were a colon (":"), simply do use the -F":" switch and argument like this:

# echo "AA:BB:CC:DD:EE:FF"|awk -F":" '{if ($(NF-2)~/D/) {print $(NF-2)}}'

...returns

DD

Note that $(NF-2) specifies the contents of the field that is two removed from the end.

Regards!

...JRF...
Rodney Hills
Honored Contributor

Re: Get a particular column value

If you are only looking for a failure, then you could do the following-

n=`autorep job | grep -c " FA "`
if [[ $n -gt 0 ]] ; then
echo "job had failure..."
fi

Since status FA will have a space before and after, we can use grep to look for that text.

HTH

-- Rod Hills
There be dragons...
Sandman!
Honored Contributor

Re: Get a particular column value

Try the following pipelined command:

# ex inputfile<> g/\/-1;/\/p
> EOF

It uses ex to filter out lines that have the string "FA" in them along with the line above it in-order to capture the jobname if the autosys output is too long and wraps around to the next line. Thereafter awk displays only those lines that either have a single field in them "jobname" or have the string "FA" embedded in it.

cheers!