从命令输出的不同位置捕获子字符串

时间:2018-08-24 01:25:13

标签: bash shell awk grep

我有一个要求,我必须从命令的输出中捕获一个字符串并将其存储以进行进一步处理。问题是命令的输出有时可能会更改,因此会导致错误的结果。


请求的数据集如下

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

我用:

cat in | grep "RUNNING" | grep "any_name" | awk '{print $1}'

生成的输出为

application_1532934978357_89483 
(se...select cast(Stage-27)
where

我想将输出生成为:

application_1532934978357_89483 
application_1532934978357_248180 
application_15329349783879_657880 

2 个答案:

答案 0 :(得分:2)

这是一个GNU awk脚本,仅捕获与单词<Switch> <Route exact to="/" component={Dashboard} key={1} />; <Route exact to="/icons" component={Icons} key={2} />; <Route to="/icons/:iconId" component={IconDetails} key={3} />; </Switch> 相关的application_XXXX

any_name

它依赖于设置为捕获每个单词的记录分隔符awk -v RS='[ \n]' '/application_[0-9_]+/{a=$0}/\<any_name\>/{print a}' file RS字符串存储在变量application_XXXX中,并在找到单​​词a时打印。

答案 1 :(得分:0)

您只需要在命令中添加一个grep:

command's output | grep "status_run" | grep -e "id_tag1" -e "id_tag2" | grep "app_id" | awk '{print $1}'

OR

awk '(/status_run/) && (/app_id*/) && (/id_tag[12]/) {print $1;}' filename

这只会打印所有带有id_tag1和id_tag2且其中包含“ status_run”的app_id。


更新问题后的解决方案:

cat filename | grep "RUNNING" | grep "any_name" | grep "application*" | awk '{print $1}'

如果要打印所有应用程序ID,请使用以下命令:

awk '/application*/{print $1}' filename