3

Maybe exclusion is not the correct term, but I'm talking about using the following in lua's string.find() function :

[^exclude]

It doesn't seem to work if the character is followed by nothing, IE it's the last character in the string.

More specifically, I'm getting a list of processes running and attempting to parse them internally with LUA.

root@OpenWrt:/# ps | grep mpd
 5427 root     21620 S    mpd /etc/mpd2.conf
 5437 root     25660 S    mpd

This wouldn't be an issue if I could expect a \n every time, but sometimes ps doesn't list itself which creates this issue. I want to match:

5437 root     25660 S    mpd

From this I will extract the PID for a kill command. I'm running an OpenWRT build that doesn't support regex or exact options on killall otherwise I'd just do that.

(%d+ root%s+%d+ S%s+mpd[^ ])

The above pattern does not work unfortunately. It's because there is no character after the last character in the last line I believe. I have also tried these:

(%d+ root%s+%d+ S%s+mpd$)

The above pattern returns nil.

(%d+ root%s+%d+ S%s+mpd[^ ]?)

The above pattern returns the first process (5427)

Maybe there is a better way to go about this, or just a simple pattern change I can make to get it to work, but I can't seem to find one that will only grab the right process. I can't go off PID or VSZ since they are variable. Maybe I'll have to see if I can compile OpenWRT with better killall support.

Anyways, thanks for taking the time to read this, and if this is a duplicate I'm sorry but I couldn't find anything similar to my predicament. Any suggestions are greatly appreciated!

William Barbosa
  • 4,936
  • 2
  • 19
  • 37
Jivex5k
  • 121
  • 14
  • Is `pgrep` available? It might help. That `$` pattern should match though. What's the looping code you have doing the match? – Etan Reisner Dec 04 '14 at 21:15
  • I have pgrep, doesn't seem to work. It returns both mpd processes. The code I have is just a javascript button that sends an http post to my openwrt router, which has LUA interactions based on the post data. So it's essentially just a button to kill mpd, but I don't want to kill mpd /etc/mpd2.conf as that is a music streamer and I only want to kill the local player. I'm not even using it to test though, i'm just ssh'd into my router running LUA interactively. – Jivex5k Dec 04 '14 at 21:23

1 Answers1

4

Given:

local s = [[5427 root     21620 S    mpd /etc/mpd2.conf
5437 root     25660 S    mpd]]

The following pattern

string.match(s,"(%d+)%s+root%s+%d+%s+S%s+mpd[%s]-$")

returns: 5437 root 25660 S mpd

whereas this:

string.match(s,"(%d+%s+root%s+%d+%s+S%s+mpd[%s]%p?[%w%p]+)")

returns:

5427 root 21620 S mpd /etc/mpd2.conf

danielgpm
  • 1,609
  • 12
  • 26
  • That did the trick! Thank you so much for your help! If I'm trying to understand why it seems the [%s]- means 0 or 1 space, not sure what the [] is for though, and the $ signifies the end of the line. – Jivex5k Dec 04 '14 at 23:01