无法从输出中 grep 查找版本

无法从输出中 grep 查找版本

我有一个脚本给出以下输出:

WVER0010I: Copyright (c) IBM Corporation 2002, 2012; All rights reserved.
WVER0012I: VersionInfo reporter version 1.15.1.48, dated 2/8/12

--------------------------------------------------------------------------------
IBM WebSphere Product Installation Status Report
--------------------------------------------------------------------------------

Report at date and time September 23, 2020 2:00:11 PM IST

Installation
--------------------------------------------------------------------------------
Product Directory        /ihs/IBM/HTTPServer
Version Directory        /ihs/IBM/HTTPServer/properties/version
DTD Directory            /ihs/IBM/HTTPServer/properties/version/dtd
Log Directory            /var/ibm/InstallationManager/logs

Product List
--------------------------------------------------------------------------------
IHS                      installed

Installed Product
--------------------------------------------------------------------------------
Name                  IBM HTTP Server for WebSphere Application Server
Version               8.5.5.10
ID                    IHS
Build Level           cf101629.01
Build Date            7/21/16
Package               com.ibm.websphere.IHS.v85_8.5.5010.20160721_0036
Architecture          x86-64 (64 bit)
Installed Features    IBM HTTP Server 64-bit with Java, Version 6
                      Core runtime

--------------------------------------------------------------------------------
End Installation Status Report
--------------------------------------------------------------------------------

我需要获取各种操作系统(例如 Aix、Linux 和 Solaris)上的版本

通过以下命令,我得到了所需的输出:

sh /ihs/IBM/HTTPServer/bin/versionInfo.sh | grep -A 1 WebSphere | tail -n 1 | awk '{print $NF}'

输出:8.5.5.10

但是,当脚本输出稍微发生变化(如下所示)时,相同的命令无法获取版本。

WVER0010I: Copyright (c) IBM Corporation 2002, 2005, 2008; All rights reserved.
WVER0012I: VersionInfo reporter version 1.15.5.1, dated 6/15/11

--------------------------------------------------------------------------------
IBM WebSphere Application Server Product Installation Status Report
--------------------------------------------------------------------------------

Report at date and time September 23, 2020 1:54:10 PM GMT+05:30

Installation
--------------------------------------------------------------------------------
Product Directory        /ihs/IBM/HTTPServer
Version Directory        /ihs/IBM/HTTPServer/properties/version
DTD Directory            /ihs/IBM/HTTPServer/properties/version/dtd
Log Directory            /ihs/IBM/HTTPServer/logs
Backup Directory         /ihs/IBM/HTTPServer/properties/version/nif/backup
TMP Directory            /tmp

Product List
--------------------------------------------------------------------------------
IHS                      installed

Installed Product
--------------------------------------------------------------------------------
Name                     IBM HTTP Server
Version                  7.0.0.19
ID                       IHS
Build Level              cf191132.09
Build Date               8/13/11
Architecture             Intel (32 bit)

--------------------------------------------------------------------------------
End Installation Status Report
--------------------------------------------------------------------------------

输出:

--------------------------------------------------------------------

然而,期望的输出是:

7.0.0.19

我期望相同的命令给出上面两个输出的版本号。

"Name"注意:我想如果我们可以搜索以后跟然后<whitespaces>再后跟的行下方的行,"IBM HTTP Server"那么我们将获得版本。然而,我无法grep得到我想要的东西。

你能建议一下吗?

答案1

awk如果您可以使用该工具,我会建议基于 - 的解决方案:

awk '/^Installed Product/{f=1} f && $1=="Version" {print $2; f=0}'

一旦找到“Installed Product”标头,就会设置一个标志f,然后(仅)查找以“Version”开头的行。它将打印该行的第二个以空格分隔的字段,即版本。作为一项安全措施,它会同时重置标志,以确保以后出现的“Version”行不会生成错误输出。

您可以将其用作

sh /ihs/IBM/HTTPServer/bin/versionInfo.sh | awk '/^Installed Product/{f=1} f && $1=="Version" {print $2; f=0}'

答案2

根据您的示例输入,您只需搜索一段数字,该数字位于行上的.单词之后,其中是该行的第一个单词,后面跟着空格,然后是数字。如果您有 GNU ,您可以简单地执行以下操作:VersionVersiongrep

sh /ihs/IBM/HTTPServer/bin/versionInfo.sh | grep -oP '^Version\s+\K\d.*'

如果您没有 GNU grep,您可以使用:

sh /ihs/IBM/HTTPServer/bin/versionInfo.sh | sed -n 's/^Version[ \t]*\([0-9]\)/\1/p'

或者,也许更清楚:

sh /ihs/IBM/HTTPServer/bin/versionInfo.sh | 
    awk '/^Version  *[0-9]/{print $2}' 

相关内容