Q:

Capture sub-string at different positions from command's output

I have a requirement, where I have to capture a string from a command's output and store it for further processing. Problem is that the command's output may change sometimes and hence this leads to erroneous results.

Requested dataset looks like
application_1532934978357_3376 app_name job_type user any_name_2 RUNNING 
UNDEFINED 10% hostname
application_1532934978357_3375 app_name job_type user any_name_2 RUNNING 
UNDEFINED 10% hostname
application_1532934978357_3374 app_name job_type user any_name_2 RUNNING 
UNDEFINED 10% hostname
application_1532934978357_249069 some_information_etc job_type any_name_2 
RUNNING UNDEFINED 95% hostname
application_1532934978357_239728 app_name job_type any_name_2 RUNNING 
UNDEFINED 10% hostname
application_1532934978357_89483 some_info job_type user any_name RUNNING 
UNDEFINED 10% hostname
application_1532934978357_248180 with prog_vrsn as
(se...select cast(Stage-27) job_type user any_name RUNNING UNDEFINED 36.1% 
hostname
application_15329349783879_657880 select cast
value ..(stage35) with table
where value=5; job_type user any_name RUNNING UNDEFINED 10% hostname

and I use:
cat in | grep "RUNNING" | grep "any_name" | awk '{print $1}'

which generates output as 
application_1532934978357_89483 
(se...select cast(Stage-27)
where

While I want to produce output as :
application_1532934978357_89483 
application_1532934978357_248180 
application_15329349783879_657880 

A:

Here is a GNU awk script that only captures the application_XXXX associated to the word any_name:
awk -v RS='[ \n]' '/application_[0-9_]+/{a=$0}/\<any_name\>/{print a}' file

It relies on the record separator RS that is set to capture each word. The application_XXXX string is stored in the variable a and printed when the word any_name is found.

