Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
bash + how to capture the version from rpm

this is the way when I try to get the Kafka version

rpm -qa | grep "^kafka_"
kafka_2_6_5_0_292-1.0.0.2.6.5.0-292.noarch

Kafka version is 1.0 , so I did the following in order to cut the Kafka version

 rpm -qa | grep "^kafka_" | sed s'/-/ /g' | awk '{print $2}' | cut -c 1-3
 1.0              <-----   results 

above cli seems to be not so elegant and long syntax

can we do it better , maybe with Perl or Python one liner command ?

over 4 years ago · Santiago Trujillo
7 Respostas
Responde à pergunta

0

You may use a single awk:

rpm -qa |
awk -F- '/^kafka_/ && split($2, a, /\./) >= 1 {print a[1] "." a[2]}'

1.0
over 4 years ago · Santiago Trujillo Relatório

0

Does this sed work?

rpm -qa | grep '^kafka_' | sed 's/[a-z0-9_]*-\(...\).*/\1/'
over 4 years ago · Santiago Trujillo Relatório

0

Refactoring your code

rpm -qa | grep "^kafka_" | sed s'/-/ /g' | awk '{print $2}' | cut -c 1-3

1st step: use AWK's FS (Field Seperator) instead preprocessing in sed

rpm -qa | grep "^kafka_" | awk 'BEGIN{FS="-"}{print $2}' | cut -c 1-3

2nd step: register {print $2} action to lines matching description rather than filtering it with grep

rpm -qa | awk 'BEGIN{FS="-"}/^kafka_/{print $2}' | cut -c 1-3

3rd step: use AWK's substr function in place of cut -c

rpm -qa | awk 'BEGIN{FS="-"}/^kafka_/{print substr($2,1,3)}'

Disclaimer: my answer assumes you want behavior exactly like your original code, even if possibly unexpected i.e. it does get first 3 characters of version parts, regardless of how many digits are in 2nd part so for example for 1.15.0.2.6.5.0-292 it does yield 1.1

over 4 years ago · Santiago Trujillo Relatório

0

if k=$(rpm -qa | grep "^kafka_")
then
  if [[ ${k#*-} =~ ^[0-9]+[.][0-9]+ ]]
  then
    k_version=$BASH_REMATCH
  else
    echo "can not determine kafka version from '$k'"
  fi
else
  echo "No kafka in rpm"
fi

The idea here is to remove everything from the version string up to the dash and then use a regexp to get the version part.

over 4 years ago · Santiago Trujillo Relatório

0

With your shown samples, please try following awk code. Simple explanation would be, sending output of rpm -qa command as a standard input to awk code. In awk program setting field aeparator to - OR . and OFS to .. Then in main program checking if a line starts from kafka_ then print 2nd and 3rd fields of it as per shown samples.

rpm -qa | awk -F'-|\\.' 'BEGIN{OFS="."} /^kafka_/{print $2,$3}'
over 4 years ago · Santiago Trujillo Relatório

0

This can be done entirely with (GNU) grep.

grep -oP '^kafka_[^-]*-\K\d+\.\d+'

For example,

$ echo kafka_2_6_5_0_292-1.0.0.2.6.5.0-292.noarch | grep -oP '^kafka_[^-]*-\K\d+\.\d+'
1.0

But since you asked for Perl or Python,

perl -ne'print "$&\n" if /^kafka_[^-]*-\K\d+\.\d+/'
over 4 years ago · Santiago Trujillo Relatório

0

Please, many tools about packages return data with different formats. rpm command is a tool where the outputs are different between the tool version and system (GNU/Linux, AIX...)...

So, when you work with rpm command in a script, never use -q option alone. Use the --queryformat (shortly --qf, with two -) to specify the output.

See man rpm for more information.

Example:

$ rpm -q --qf "%{NAME}:%{VERSION}" firefox
firefox:91.0.1

Tags are specified like this: %{...}

Tag names are obtained with rpm --querytags command.

Example with unknow/partial name of package:

$ rpm -qa --qf "%{NAME}:%{VERSION}\n" | grep '^kernel'
kernel-srpm-macros:1.0
kernel-headers:5.13.3
kernel-core:5.13.12
kernel-modules:5.13.12
kernel:5.13.12
kernel-modules-extra:5.13.12

Note:

  • \n is used to separate all packages 'name:version' couples
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda