例如,
假设我有 logfile.txt 其中包含
“这是一个示例文本文件”
我的模式是“sample” 如何在我的 logfile.txt 中获得 sample 旁边的单词。
最佳答案
这是使用 awk 执行此操作的一种方法:
$ awk '{for(i=1;i<=NF;i++)if($i=="sample")print $(i+1)}' file
text
解释:
$ awk '{
for(i=1;i<=NF;i++) # process every word
if($i=="sample") # if word is sample
print $(i+1) # print the next
}' file
和 sed:
$ sed -n 's/.* sample \([^ ]*\).*/\1/p' file
text
IE。之后
sample
下一个空格分隔的字符串和 grep 使用 PCRE 和正面看后面:
$ grep -oP '(?<=sample )[^ ]*' file
text
见前面的解释。
关于linux - 如何使用 grep、sed 和 awk 打印找到的模式后的下一个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48606867/