Regex or split in python for awk shell equivalent

I have an agent version file that I need to parse to get version information about the application. The contents (example) of the version file is /opt/app_folder/agent_version.txtas follows:

Version: 10.2.4.110
Pkg name: XXXX-10.2.4-Agent-Linux-x86_64
Revision: 110
Patch version: 23

I need a conclusion as the 1st 3rd number of Versionand the number of Release version. For example, for example:

Current Version: 10.2.4.23

So, I used the following to achieve this in a shell using awk

FILE=/opt/app_folder/agent_version.txt

my_ver=`awk -F[:.] '/Version/ {gsub(" ",""); print $2"."$3"."$4}' ${FILE}`
            OR
my_ver=`awk -F[-] '/Pkg/ {print $2}' ${FILE}`

my_patch=`awk -F[:.] '/version/ {gsub(" ",""); print $NF}' ${FILE}`
my_cur_ver="$my_ver.$my_patch"

echo $my_cur_ver
10.2.4.23

How to achieve this result in Python? Use regex or split or a combination of both?

I use Python 3.3onRHEL 6.2 x86_64

+4
source share
4 answers

Assuming it txtcontains the contents of the file, you get the version:

import re
version = re.findall("Version:\s+((?:\d+\.){3})", txt)[0] + re.findall("Patch version:\s+(\d+)", txt)[0]

:

version = ''.join(re.findall("Version:\s+((?:\d+\.){3}).*Patch version:\s+(\d+)", txt, re.DOTALL)[0])
+1

awk .

awk '/Version/{split($NF,a,".");next} /Patch version/{print a[1],a[2],a[3],$NF}' OFS="."  Input_file

.

10.2.4.23
+2

Or parse it in a dict and extract the necessary parts:

txt = """Version: 10.2.4.110
Pkg name: XXXX-10.2.4-Agent-Linux-x86_64
Revision: 110
Patch version: 23"""  

# with open("yourfile.txt") as f: 
#     txt = f.read()

dic = {}
for l in txt.splitlines():   # split the blob into lines
    k,v = l.split(":",2)     # split at first : produce 2 items max
    dic.setdefault( k.strip(),v.strip().split("."))  # add to dict & split at . into list

v =  '.'.join(dic["Version"][:-1]+dic["Patch version"] ) # join correct things together 

print(v)

Conclusion:

10.2.4.23

Overall a little wasteful, but works without regular expression.

Just for completeness: dicit looks like this:

{'Revision': ['110'], 
 'Patch version': ['23'], 
 'Version': ['10', '2', '4', '110'], 
 'Pkg name': ['XXXX-10', '2', '4-Agent-Linux-x86_64']}
+2
source

Regex :(?:Version:\s?((?:\d+\.){3})(?:[^\r\n]+\r?\n){3}Patch version:\s?(\d+))

Substitution :$1$2

Match 1
....
Group 1.    9-16    `10.2.4.`
Group 2.    90-92   `23`

Output

10.2.4.23

Regex Demo

import re

text = 'Version: 10.2.4.110\r\nPkg name: XXXX-10.2.4-Agent-Linux-x86_64\r\nRevision: 110\r\nPatch version: 23'

replaced = re.sub(r'(?:Version:\s?((?:\d+\.){3})(?:[^\r\n]+\r?\n){3}Patch version:\s?(\d+))', '\g<1>\g<2>', text)
print(replaced) //10.2.4.23
+1
source

Source: https://habr.com/ru/post/1692879/


All Articles