Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex or split in python for shell awk equivalent

I've a agent version file that I need to parse to get the application version details. The (example) contents of version file /opt/app_folder/agent_version.txt is as below:

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

I need the output as 1st 3 numbers from Version and the number from Release version. For e.g.:

Current Version: 10.2.4.23

So, I've used the below to achieve this in 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 do I achieve this result in Python? Use regex or split or a combination of both?

I'm using Python 3.3 on RHEL 6.2 x86_64

like image 657
Marcos Avatar asked Jan 29 '18 15:01

Marcos


4 Answers

Following awk may help you in same.

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

Output will be as follows.

10.2.4.23
like image 105
RavinderSingh13 Avatar answered Sep 18 '22 04:09

RavinderSingh13


Or parse it into a dict and retrieve the needed 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)

Output:

10.2.4.23

It is overall a bit wastefull, but works w/o regex.

Just for completeness: dic looks like this:

{'Revision': ['110'], 
 'Patch version': ['23'], 
 'Version': ['10', '2', '4', '110'], 
 'Pkg name': ['XXXX-10', '2', '4-Agent-Linux-x86_64']}
like image 44
Patrick Artner Avatar answered Sep 21 '22 04:09

Patrick Artner


assuming txt holds the contents of the file, this would get you the version:

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

Or if you prefer to only have one regular expression:

version = ''.join(re.findall("Version:\s+((?:\d+\.){3}).*Patch version:\s+(\d+)", txt, re.DOTALL)[0])
like image 29
talz Avatar answered Sep 22 '22 04:09

talz


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
like image 44
Srdjan M. Avatar answered Sep 21 '22 04:09

Srdjan M.