I would like to have some help writing a script (which should work on FreeBSD where I have sh as default) that should grab a precise string when I do a ps ax | grep bhyve command from the terminal. For example :

# ps ax | grep bhyve

43137  -  Is       0:00.00 bhyve: system.pwd (bhyve)
43138  -  Is       0:00.00 bhyve: system.grp (bhyve)
43132  0  IC       2:06.86 bhyve: vm0:18 (bhyve)
43234  1  D+       0:00.00 grep bhyve (csh)

when I do :

ps ax | awk '/bhyve: vm/{print $6}'

it gives :


when I do :

ps ax | awk '/bhyve: [vm]/{print $6}'

it gives :


but they are both wrong. The result should be : 0:18


  • If I was only running this a few times, or did not care about performance (and if I did then I would do it in Rust) then I would take what I had and pipe it into sed to do the last bit. Aug 31, 2022 at 18:48
  • something like ps axo time,command will get you exactly the columns you want, in the order you want them in. but I'd proably write it in C if I used the program a lot
    – thrig
    Aug 31, 2022 at 19:04
  • You're still doing the same thing we showed you how to fix when you asked this question a couple of days ago at unix.stackexchange.com/q/715477/133219, i.e. using a regexp that matches itself. Please read and do what it shows in the question yours was closed as a dup of.
    – Ed Morton
    Aug 31, 2022 at 20:39
  • First of all,the solution proposed didn't work well and 2) it hasn't explained well. So,I tried to apply it by myself,but I've realized that it didn't work well maybe because to hide part of the string is a solution which works only in some circumstances. For these reasons,I've opened another post and I've found a more precise solution. I suggest you to give more detailed explanation when you sugggest something and a more accurate support,otherwise one looks for a better solution. maybe you are sure to talk with an experienced user and you give a fast reply,but it's not always true.
    – Marietto
    Sep 1, 2022 at 22:58
  • I suggest you to don't close one question even if it is closer to the other one,because as I said,the trick to hide part of the string is not a solution which works in a lot of conditions,people should understand this. It's a good idea that an alternative solution is kept alive when the first one does not work. To have different alternative solutions should be considered a good situation. To close one post only because it's close to another one (but not the same,because I've got an answer that works differently),is not a good idea.
    – Marietto
    Sep 1, 2022 at 23:03

1 Answer 1


This code may work (stripping first two symbols from the string):

ps ax | awk '/bhyve: [vm]/{print substr($6,3)}'
  • 0:18. it works. very thanks. if ps ax | awk '/bhyve: vm/{print $6}' would give vm0:9,how your script should be modified ?
    – Marietto
    Aug 31, 2022 at 19:13
  • 1
    it works even if the starting input is vm0:9,that's good.
    – Marietto
    Aug 31, 2022 at 19:30

Not the answer you're looking for? Browse other questions tagged .