Help: Efficient regular expression

On Behalf Of Divya B.:

string = "root 14051 14033 3 08:39 pts/2 00:00:00 /bin/bash

-x -s"

it fetches only -s for me.

sec, last = string.split.values_at(1, -1)

doesnt work for the same reason

i need everything after 00.00.00 till the end

i.e., /bin/bash -x -s

you can modify the non-regex solutions since they are just plain array
manipulations

irb(main):003:0> str = “root 14051 14033 3 08:39 pts/2 00:00:00
/bin/bas
h -x -s”
=> “root 14051 14033 3 08:39 pts/2 00:00:00 /bin/bash -x -s”
irb(main):006:0> astr = str.split
=> [“root”, “14051”, “14033”, “3”, “08:39”, “pts/2”, “00:00:00”,
“/bin/bash”, “-
x”, “-s”]
irb(main):007:0> pid = astr[1]
=> “14051”
irb(main):010:0> cmd = astr[7…-1].join " "
=> “/bin/bash -x -s”

or

irb(main):024:0> pid,*cmd = astr.values_at(1,7…-1)
=> [“14051”, “/bin/bash”, “-x”, “-s”]
irb(main):025:0> pid
=> “14051”
irb(main):026:0> cmd
=> ["/bin/bash", “-x”, “-s”]
irb(main):027:0> cmd = cmd.join " "
=> “/bin/bash -x -s”

On 7/10/07, Divya B. [email protected] wrote:

cmd = string[/\s(\S+)$/, 1]
doesnt fetch me anything:)
No idea that was not my code :slight_smile:

program=string.split.last
what if
string = “root 14051 14033 3 08:39 pts/2 00:00:00 /bin/bash -x
-s”
it fetches only -s for me.
As simple as possible, but not simpler. Now we are in the simpler case
:slight_smile:
But see below

program=string[/[a-z/]+$/]
the command column mauy start with character. i dont want to limit it in
my regexp. it has to be generic.

with all your comments, i tried
pid = run_process[/\s(\d+)/, 1]
cmd = run_process[/:\d+:\d+\s(\S.*)\s$/, 1]
Does not work the :\d+ stuff might be a parameter of the program :frowning:

is there any other way?
Yep :slight_smile: counting fields after all
x = split
y= x[1]
z = x[7…-1].join(" ")

There might still be a pitfall though in case of some old processes
where you will have to analyse if the date takes one or two fields if
memory serves.

HTH
Robert

On 7/10/07, Gregory B. [email protected] wrote:

On 7/10/07, Robert D. [email protected] wrote:

On 7/10/07, James Edward G. II [email protected] wrote:

On Jul 10, 2007, at 3:25 PM, Divya B. wrote:

> > Very interesting James, I seem to be rather extreme and > > > > sec, last = string.split.values_at(1, -1) > > might be a tad to long for one line in your style, however Ruby syntax > > just supports this marvelous syntax :) > > > > sec, last = string.split. > > values_at(1, -1) > > What is your terminal width, 30?

oops wrong thread, I was caught in the “Beautiful Code Thread”.

Sorry
Robert

From: Peña, Botp [mailto:[email protected]]

On Behalf Of Divya B.:

# string = "root 14051 14033 3 08:39 pts/2 00:00:00

/bin/bash -x -s"

# it fetches only -s for me.

# sec, last = string.split.values_at(1, -1)

# doesnt work for the same reason

# i need everything after 00.00.00 till the end

# i.e., /bin/bash -x -s

you can modify the non-regex solutions since they are just

plain array manipulations

irb(main):003:0> str = "root 14051 14033 3 08:39 pts/2

00:00:00 /bin/bas

h -x -s"

=> “root 14051 14033 3 08:39 pts/2 00:00:00 /bin/bash -x -s”

irb(main):006:0> astr = str.split

=> [“root”, “14051”, “14033”, “3”, “08:39”, “pts/2”,

“00:00:00”, “/bin/bash”, "-

x", “-s”]

irb(main):007:0> pid = astr[1]

=> “14051”

irb(main):010:0> cmd = astr[7…-1].join " "

=> “/bin/bash -x -s”

or

irb(main):024:0> pid,*cmd = astr.values_at(1,7…-1)

=> [“14051”, “/bin/bash”, “-x”, “-s”]

irb(main):025:0> pid

=> “14051”

irb(main):026:0> cmd

=> [“/bin/bash”, “-x”, “-s”]

irb(main):027:0> cmd = cmd.join " "

=> “/bin/bash -x -s”

i forgot to add the last one; it’s niftier, so i would not want you to
miss it.

irb(main):060:0> pid,cmd=str.split(" ",8).values_at(1,7)
=> [“14051”, “/bin/bash -x -s”]
irb(main):061:0> pid
=> “14051”
irb(main):062:0> cmd
=> “/bin/bash -x -s”

i totally forgot about the second param of split. ruby is cool, indeed
:slight_smile:
kind regards -botp

2007/7/10, Divya B. [email protected]:

string =~ /(\d+)\s+(\d+)\s+\d+\s+\d+:\d+\s+.\s+\d+:\d+:\d+\s+(.)\s/

i know this is not the efficient way of doing it.

Your regexp isn’t too bad. With only a little tweaking I get this
which is not too inefficient IMHO:

irb(main):001:0> s = “root 14051 14033 3 08:39 pts/2 00:00:00
/bin/bash”
=> “root 14051 14033 3 08:39 pts/2 00:00:00 /bin/bash”
irb(main):004:0> s =~
/^\S+\s+(\d+)\s+\d+\s+\d+\s+\d+:\d+\s+\S+\s+\d+:\d+:\d+\s+(\S+)/
=> 0
irb(main):005:0> [$1, $2]
=> [“14051”, “/bin/bash”]

Or you can do

irb(main):007:0> s =~ /^\S+\s+(\d+)\s+(?:\S+\s+){5}(\S+)/
=> 0
irb(main):008:0> [$1, $2]
=> [“14051”, “/bin/bash”]
irb(main):011:0> /^\S+\s+(\d+)\s+(?:\S+\s+){5}(\S+)/.match(s)[1…-1]
=> [“14051”, “/bin/bash”]

Kind regards

robert

You can process the ps output using other commands.

ps -aef|tr -s ’ '|cut -d ’ ’ -f2,8-

This will just print your second column the 8th and anything that
comes after. At this point your regexp only need to split the string
at the first space.

Paolo

On 7/11/07, Paolo N. [email protected] wrote:

You can process the ps output using other commands.

ps -aef|tr -s ’ '|cut -d ’ ’ -f2,8-

This will just print your second column the 8th and anything that
comes after. At this point your regexp only need to split the string
at the first space.

You’re in trouble if any of your fields have spaces, though the
specific case of ps -aef looks safe enough.

martin