在同一行的特定单词之后提取一个单词

编程入门 行业动态 更新时间:2024-10-09 09:19:12
本文介绍了在同一行的特定单词之后提取一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

在同一行的特定单词之后提取一个单词

本文发布于:2023-10-31 10:17:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545786.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!