You are not logged in.

#1 2013-02-11 16:58:33

welpert
Member
From: the Netherlands
Registered: 2011-01-11
Posts: 73

Match between two strings of which one may occur often in python

I get a large swab of output from pacman -Qi. This output is piped using subprocess and converted to a string using str.decode. This all works.

But then I would like to be able to cut a a block related to one package out of that large swap of output, like this:

Name           : steam
Version        : 1.0.0.25-3
URL            : http://steampowered.com/
Licenses       : custom
Groups         : None
Provides       : None
Depends On     : bash  desktop-file-utils  hicolor-icon-theme  curl  dbus
                 freetype2  gdk-pixbuf2  ttf-font  zenity  lib32-libgl
                 lib32-gcc-libs  lib32-libx11
Optional Deps  : lib32-ati-dri: for open source ATI driver users
                 lib32-catalyst-utils: for AMD Catalyst users
                 lib32-intel-dri: for open source Intel driver users
                 lib32-nouveau-dri: for Nouveau users
                 lib32-nvidia-utils: for NVIDIA proprietary blob users
                 lib32-flashplugin: for flash video, copy to
                 $XDG_DATA_HOME/Steam/ubuntu12_32/plugins/
Required By    : None
Conflicts With : None
Replaces       : None
Installed Size : 6424.00 KiB
Packager       : Daniel Wallace <danielwallace at gtmanfred dot com>
Architecture   : x86_64
Build Date     : Fri Feb 8 21:51:42 2013
Install Date   : Mon Feb 11 11:35:09 2013
Install Reason : Explicitly installed
Install Script : Yes
Description    : Digital distribution client - open beta - bootstrap package

The goal is to search the large swab of output for a package installed on a specific install date.

I've tried many kinds of pattern matching but I cant seem to wrap my head around it. Does anyone know a short, sweet, fast, clean way to do this?

Greetings

Last edited by welpert (2013-02-11 16:59:24)


“Great art is horseshit, buy tacos.” - Charles Bukowski

freenode/archlinux: nl-trisk

Offline

#2 2013-02-11 19:47:02

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 216

Re: Match between two strings of which one may occur often in python

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import re, sys

pacman_data = sys.stdin.readlines()

for line in pacman_data:
	match = re.search( "^install date[ \t]*: (.*)", line, re.I )
	if match: print( match.group( 1 ))

Offline

#3 2013-02-11 20:06:09

kaszak696
Member
Registered: 2009-05-26
Posts: 543

Re: Match between two strings of which one may occur often in python

Since output of pacman -Qi is separated by an empty line, you can split the resulting output by

output.split('\n\n')

and deal with them as a list one by one or even build a dictionary.


'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard

Offline

#4 2013-02-11 20:16:29

welpert
Member
From: the Netherlands
Registered: 2011-01-11
Posts: 73

Re: Match between two strings of which one may occur often in python

Thanks!

Might need to get more into python. I was fiddling around with it in the wrong way i suppose.

I'll have a go on wednesday.

Cheers!


“Great art is horseshit, buy tacos.” - Charles Bukowski

freenode/archlinux: nl-trisk

Offline

Board footer

Powered by FluxBB