首页 > 解决方案 > grep 某个单词,其前 10 个字符从 20 个字符中已知,最后 10 个字符是动态的

问题描述

我正在运行一个内部调用 python 文件的 abcd.sh 文件,python 脚本给出了一些输出,我正在接受一些变量并尝试 grep 一些单词,其起始 10 个字符从 20 个字符中已知,最后 10 个字符是动态的,这个词是多次的。

我想grep这个词。例如:在下面的日志示例中,我想搜索修复的"Request-Id-12977-1233"位置"Request-Id"

1::com.demo.log.Demo::main::Sat Dec 15 01:42:43 EST 2012:: requestID: Request-Id-12977-1233  Demo::main
1::com.demo.log.Demo::main::Sat Dec 2012::  requestID: Request-Id-12977-1233  Demo::main
1::com.demo.log.Demo::main:: 2012:: requestID: Request-Id-12977-1233  Demo::main
1::com.demo.log.Demo::main:  requestID: Request-Id-12977-1233  Demo::main

标签: linuxshellgrepfindsh

解决方案


我终于得到了答案,我之前尝试过这个但没有成功,可能是那里有一些错误。--获取python脚本的响应

initiateResponse=$(python pythonscript.py argument1 "argument2" "arguments3")

-- 回显输出,然后管道 grep,然后管道尾取最后一个值,因为有多个

requestId=$(echo initiateReposnse | grep -o "Request-Id-\w*" | tail -n 1)
echo "request Id $ requestId"

推荐阅读