如何提取Linux(csh)中某个特定单词之后的单词? 更确切地说,我有一个文件,其中只有一行看起来像这样:
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:
[some useless data] --pe_cnt 100 --rd_cnt 1000 [some more data]我想提取在--pe_cnt单词后面的数字 100 . 我不能使用sed,因为只有当您要提取整行时才可以使用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 want to 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更多推荐
在同一行的特定单词之后提取一个单词
发布评论