
Eu tenho um script que fornece a saída abaixo:
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
--------------------------------------------------------------------------------
Preciso obter a versão em todos os tipos de sistemas operacionais como Aix, Linux e Solaris
Com o comando abaixo estou obtendo a saída desejada:
sh /ihs/IBM/HTTPServer/bin/versionInfo.sh | grep -A 1 WebSphere | tail -n 1 | awk '{print $NF}'
Saída: 8.5.5.10
No entanto, quando a saída do script muda ligeiramente como abaixo, o mesmo comando não fornece a versão.
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
--------------------------------------------------------------------------------
Saída:
--------------------------------------------------------------------
No entanto, a saída desejada é:
7.0.0.19
Estou esperando que o mesmo comando forneça o número da versão para a saída acima.
Nota: Acho que se pudermos procurar a linha abaixo da linha começando com "Name"
seguido por <whitespaces>
e depois seguido por "IBM HTTP Server"
, obteremos a versão. No entanto, não consigo grep
obter a saída desejada.
Você pode sugerir?
Responder1
Eu sugeriria uma awk
solução baseada em -se essa ferramenta estiver disponível para você:
awk '/^Installed Product/{f=1} f && $1=="Version" {print $2; f=0}'
definirá um sinalizador f
assim que o cabeçalho "Produto instalado" for encontrado e (somente) procurará uma linha começando com "Versão". Ele imprimirá o segundo campo separado por espaços em branco dessa linha, que é a versão. Como medida de segurança, ele redefinirá ao mesmo tempo o sinalizador para garantir que nenhuma ocorrência posterior de uma linha "Versão" gere uma saída falsa.
Você pode usá-lo como
sh /ihs/IBM/HTTPServer/bin/versionInfo.sh | awk '/^Installed Product/{f=1} f && $1=="Version" {print $2; f=0}'
Responder2
Com base na sua entrada de exemplo, você está simplesmente pesquisando o trecho de números e .
que vem depois da palavra Version
nas linhas onde Version
está a primeira palavra da linha e onde é seguida por um espaço em branco e depois um número. Se você possui GNU grep
, você pode simplesmente fazer:
sh /ihs/IBM/HTTPServer/bin/versionInfo.sh | grep -oP '^Version\s+\K\d.*'
Se você não possui GNU grep
, você pode usar:
sh /ihs/IBM/HTTPServer/bin/versionInfo.sh | sed -n 's/^Version[ \t]*\([0-9]\)/\1/p'
Ou, talvez de forma mais legível:
sh /ihs/IBM/HTTPServer/bin/versionInfo.sh |
awk '/^Version *[0-9]/{print $2}'