1826235 Members
3541 Online
109692 Solutions
New Discussion

Awk help

 
SOLVED
Go to solution
tom quach_1
Super Advisor

Awk help

Hi All,
sql statement returned 2 columns and i like to get the result of a second column " RUNNING" using awk '{print $2}'

but because of space between the words. the result was not correct.

Please give some suggestions on how to use "awk" or "cut" to get the result which is the
"RUNNING"

Thanks in advance.


Workflow Deferred Agent Listener RUNNING
Workflow Deferred Notification Agent Listener RUNNING
Workflow Error Agent Listener RUNNING
Workflow Inbound JMS Agent Listener RUNNING
Workflow Inbound Notifications Agent Listener RUNNING
Workflow Java Deferred Agent Listener RUNNING
Workflow Java Error Agent Listener RUNNING
5 REPLIES 5
Dennis Handly
Acclaimed Contributor
Solution

Re: Awk help

>but because of space between the words. the result was not correct.

You are going to have to provide more details of how the fields are defined. Unless you always want the last column?

You can use awk to print the last column, $NF. And work backward, $(NF-1).

Otherwise you may have to use advanced AI technology to determine where your fields begin and end.
Jose Mosquera
Honored Contributor

Re: Awk help

Hi,

I've noticed that in all lines always appear "Agent Listener " before "RUNNING", so you can use it as field limiter. eg:
#cat sample.txt
Workflow Deferred Agent Listener RUNNING
Workflow Deferred Notification Agent Listener RUNNING
Workflow Error Agent Listener RUNNING
Workflow Inbound JMS Agent Listener RUNNING
Workflow Inbound Notifications Agent Listener RUNNING
Workflow Java Deferred Agent Listener RUNNING
Workflow Java Error Agent Listener RUNNING

#cat sample.txt |awk -F"Agent Listener " '{ print $2 }'
RUNNING
RUNNING
RUNNING
RUNNING
RUNNING
RUNNING
RUNNING

I hope it will be useful.

Rgds.
Bill Hassell
Honored Contributor

Re: Awk help

If you simply want the last item on each line:

awk '{print $NF}' /tmp/mysql.out


Bill Hassell, sysadmin
Raj D.
Honored Contributor

Re: Awk help

Tom,
You can try this,

# awk '{for (i=0;i<=NF;++i) if ($i ~ "RUNNING") print $0 }' file

It will print all the line contains RUNNING .

Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
tom quach_1
Super Advisor

Re: Awk help


Thank you all for your helps.
Regards,