Linux的:在同一行特定的字后,提取一个字 [英] linux: Extract one word after a specific word on the same line
问题描述
我是一个新的Linux用户,我学会了自我,所以我很抱歉,如果这questtion是愚蠢的。我没有在网上搜索,但无法找到一个合适的答案..
我怎样才能提取的Linux(CSH)特定的词之后,是词?
更多precisely我有有一个单一的线,看起来像这样的文件:
I am a new linux user and I'm self learned, so I'm sorry if this questtion is stupid. I did search online but couldnt find a proper answer.. How can I extract a word that comes after a specific word in Linux (csh)? More precisely I have a file which has a single line which looks like this:
的 [一些无用的数据] 的--pe_cnt的 100 --rd_cnt 1000的 [更多数据] 的
[some useless data] --pe_cnt 100 --rd_cnt 1000 [some more data]
我想提取数 100 这是--pe_cnt单词之后。
我不能用'sed的'作为唯一的,如果你想提取一整行的作品。也许我可以用'AWK?
I want to extract the number 100 which is after the --pe_cnt word. I cannot use 'sed' as that works only if you wanna extract an entire line. Maybe I can use 'awk'?
另外,我有具有不同值,而不是100,所以我需要的东西,其提取的价值,但不依赖于该值的多个文件。
Also, I have multiple files that have different values instead of 100 so I need something that extracts the value but doesn't depend on the value.
推荐答案
通过 AWK
:
awk '{for(i=1;i<=NF;i++) if ($i=="--pe_cnt") print $(i+1)}' inputFile
基本上环过线的每一个字。当你发现第一个你正在寻找,抓住下一个单词并打印。
Basically loop over each word of the line. When you find the first you are looking for, grab the next word and print it.
使用的grep
:
grep -oP "(?<=--pe_cnt )[^ ]+" inputFile
这篇关于Linux的:在同一行特定的字后,提取一个字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!