在同一行的特定单词之后提取一个单词 [英] Extract one word after a specific word on the same line

查看:67
本文介绍了在同一行的特定单词之后提取一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何提取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

这篇关于在同一行的特定单词之后提取一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆