5

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

Marcos
  • 845
  • 3
  • 10
  • 21
  • you should include your Python efforts to solve using `regex/split` as well to question... because that is your problem statement.. bash/awk solution is only extra information.. – Sundeep Jan 29 '18 at 15:27
  • @talz it's only been an hour since he asked the question - give the guy a few hours at least to see what answers he gets before having to select one! – Ed Morton Jan 29 '18 at 16:56
  • @Sundeep , I didn't try much..almost to the point of 'Not at all', as I'm a 'Very' Noobie in python and didn't knew how to proceed :) – Marcos Jan 31 '18 at 14:40

4 Answers4

2

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
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
2

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']}
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
1

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])
talz
  • 1,004
  • 9
  • 22
1

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
Srdjan M.
  • 3,310
  • 3
  • 13
  • 34
  • Do we really need \r\n in regex instead of \n, as I&#39;ll be running the code only on Linux hosts...Never on Windows...The regex doesn't seem to work on my Linux hosts – Marcos Feb 08 '18 at 06:32