Solved

regex matching - multiple lines

Posted on 2007-03-28
8
759 Views
Last Modified: 2013-12-26
I've been scratching my head a bit on this one (regex isn't my strong suit).  I am assigning output from a executable to a variable in my script as so:

data=`$XM list -l | sed s/\(//g | sed s/\)//g`; #strip parens

I want to pull out lines matching:

domid
cpu_time

from $data, but I can't figure out exactly how to go about that via a regex.  As the input is not coming in via a file, I don't know how to grab one line at a time and I can't save the output temporarily to a file as that is one of my restrictions.  There are multiple occurrences of the below block in the output I need to parse and I don't know beforehand how many there will be.

How can I take this info and parse it into an array holding the tokens?  The tokens being defined as the line beginning with domid and the line beginning with cpu_time.  I'm under the impression that bash does not have multi-dimensional arrays so I have to store these in 2 arrays, right?

(domain
    (domid 14)
    (uuid 04500ade-a703-23b7-e6f8-31e41c588c00)
    (vcpus 1)
    (cpu_weight 1.0)
    (memory 160)
    (shadow_memory 0)
    (maxmem 160)
    (features )
    (name sampledomain.com)
    (on_poweroff destroy)
    (on_reboot restart)
    (on_crash destroy)
    (image
        (linux
            (kernel /home/users/sampledomain.com/linux)
            (root '/dev/xvda1 ro')
        )
    )
    (device
        (vif
            (backend 0)
            (script vif-bridge)
            (bridge xen-br0)
            (mac aa:00:79:64:33:ce)
        )
    )
    (device
        (vbd
            (backend 0)
            (dev xvda1:disk)
            (uname file:/home/users/sampledomain.com/fc6-1.ext3)
            (mode w)
        )
    )
    (device
        (vbd
            (backend 0)
            (dev xvda9:disk)
            (uname file:/home/users/sampledomain.com/swapfs.swp)
            (mode w)
        )
    )
    (state -b----)
    (shutdown_reason poweroff)
    (cpu_time 20205.5002826)
    (online_vcpus 1)
    (up_time 3047639.48897)
    (start_time 1171852894.06)
)
0
Comment
Question by:lomidien
8 Comments
 
LVL 10

Expert Comment

by:Jaax
ID: 18807671
Your regex would be /[^[domain][cpu_time]]/
0
 

Author Comment

by:lomidien
ID: 18808017
The line that I'm parsing is the line above, but with 10-50 repetitions.  There are no line endings because I've stripped \n using sed.  How can I apply the above to tokenize entries of:

domid & cpu_time

and stuff them into an array(s) so that I can retried them like:

dom[n] & cpu[n]

It may be that I'm going about this wrong, I'm not sure, but I'm trying to put this in a bash script like so:

(irrelevant lines stripped)

#!/bin/bash

#SCRIPT VARIABLES
XM="/usr/sbin/xm"; #location of xm executable

#GRAB CURRENT XEN STATISTICS (PARENS & \n STRIPPED)
data=`$XM list -l | sed s/\(//g | sed s/\)//g`;

#i want to iterate over the input (like the above block) and pull
#substrings matching "domid NUMBER" and "cpu_time NUMBER" and 'tokenize'
#those into an array here

exit

I hope that this is clear, I'm a java programmer by nature and bash scripting isn't exactly my forte. :)
0
 
LVL 84

Expert Comment

by:ozo
ID: 18809713
dom=(`$XM list -l | sed s/\(//g | sed s/\)//g | grep domid`)
cpu=(`$XM list -l | sed s/\(//g | sed s/\)//g | grep cpu_time`)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:lomidien
ID: 18814274
ozo:

That works assuming that there is only 1 block like I posted.  The input consists of this block repeated as many as 50 times and I need to pull out each match.  I want to stuff the matching portions into an array where each element will hold a single matching entry.

Thanks,
David
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 18817272
dom=(`$XM list -l | sed s/\(//g | sed s/\)//g | grep domid`) does stuff the matching portions into an array where each element will hold a single matching entry.
echo ${dom[0]} would show the first entry ${dom[*]} would show all ig them
0
 
LVL 6

Expert Comment

by:Mehuge
ID: 18990291
# Assumes well formatted input
$XM list -l  | awk > /tmp/blah$$ '
    /^[     ]*\(domid/ {
        gsub("\\(",""); gsub(")","");
        id="domid[" n++ "]=" $2;
    }
    /^[     ]*\(cpu_time / {
        gsub("\\(",""); gsub(")","");
        time="cpu_time[" n "]=" $2;
    }
    /^[     ]*\(domain/ && id != "" {
        print id "; " time;
        id=""; time="";
    }
    END {
        if (id != "") {
            print id "; " time;
        }
    }
    '
. /tmp/blah$$

echo ${domid[0]} etc.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Powershell regex followup 13 76
linux pull logs and export FTP 4 78
Check for Linux process in script 7 48
AWK: Pythagoras script 6 22
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question