[英]grep pattern from command output
I want to run the following in Linux via execute shell in jenkins.我想通过 jenkins 中的执行 shell 在 Linux 中运行以下命令。
when I run it directly in Linux, I get:当我直接在 Linux 中运行它时,我得到:
$ polyspace-access -list-project XX/YY -host MyHost -protocol http -login MyUsername -encrypted-password MyPassword
Connecting to MyHost
Connecting as MyUsername
Get project list with the last Run Id
XX/YY RUN_ID 10
Command Completed
I can run the same command in Jenkins, but --我可以在 Jenkins 中运行相同的命令,但是——
export RunID=`polyspace-access -list-project XX/YY -host MyHost -protocol http -login MyUsername -encrypted-password MyPassword`
echo "This is my RunID: $RunID"
... and here is the result: ...结果如下:
15:06:59 This is my RuleID: Connecting to MyHost 15:06:59 Connecting as MyUsername 15:06:59 Get project list with the last Run Id 15:06:59 XX/YY RUN_ID 10 15:06:59 Command Completed
15:06:59 这是我的 RuleID:连接到 MyHost 15:06:59 以 MyUsername 身份连接 15:06:59 获取具有最后一个运行 ID 的项目列表 15:06:59 XX/YY RUN_ID 10 15:06:59 命令完全的
How can I read only the RUN_ID number from the results?如何从结果中只读取 RUN_ID 号?
If your shell has awk
available, you could pipe the output through it to ask for the third field in the line that contains the text "RUN_ID":如果你的 shell 有可用的
awk
,你可以通过它来管道输出以询问包含文本“RUN_ID”的行中的第三个字段:
runid=`polyspace-access -list-project XX/YY -host MyHost -protocol http -login MyUsername -encrypted-password MyPassword | awk '/RUN_ID/ { print $3 }'`
echo "This is my RunID: $runid"
You could adjust the pattern matching to be stricter if you know that XX/YY
is a fixed string.如果您知道
XX/YY
是固定字符串,则可以将模式匹配调整为更严格。 You could also adjust the printing to $NF
if there might be more than three fields but the desired number is always the last field.如果可能有超过三个字段但所需的数字始终是最后一个字段,您也可以将打印调整为
$NF
。
You can do this with some groovy.你可以用一些 groovy 来做到这一点。 Please check the following sample pipeline.
请检查以下示例管道。 I'm just echoing your output in the script block.
我只是在脚本块中回显您的输出。
pipeline {
agent any
stages {
stage('GetID') {
steps {
script {
string = sh(returnStdout: true, script: 'echo "15:06:59 This is RUN10X my RuleID: Connecting to MyHost 15:06:59 Connecting as MyUsername 15:06:59 Get project list with the last Run Id 15:06:59 XX/YY RUN_ID 10 15:06:59 Command Completed"').trim()
def runId = string.split("RUN_ID")[1].split(" ")[1]
echo "$runId"
}
}
}
}
}
Note: You may need to improve the script part to handle error cases.注意:您可能需要改进脚本部分以处理错误情况。
if you want to generalize it, such that "RUN_ID"
has spaces around it, and always print out the last column, try :如果您想对其进行概括,例如
"RUN_ID"
周围有空格,并且始终打印出最后一列,请尝试:
# (broken apart for readability - it's one single string)
echo 'abc def -$314,159,265,358,979,323.84 XX/YY 2277118.822881188'
' RUN_ID 3321928094887362347870319429489390175' |
{m,g}awk '$!NF=$(NF*=!_!~NF)' FS=' RUN[_]ID '
gawk '$_=$(NF*=!_<NF)' FS=' RUN[_]ID '
mawk '$!++NF=$--NF' FS=' RUN[_]ID '
. .
3321928094887362347870319429489390175
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.