[SOLVED] How to create a sh script that can give as output the exact string I want using the awk command
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to create a sh script that can give as output the exact string I want using the awk command
Hello.
I would like to have some help writing a script (which should works 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 :
The thread is marked as solved, and unsure the value of all these variations? If efficiency is a goal then one should be going to the source of the information, and since it occurs in the "command" column, that's not likely to be ps?
Checking for what bhyve is, it's a misspelling of "beehive", and the name of virtual machine software running (according to post #1) on FreeBSD.
I can't easily find what the vm0:18 represents, but suspect there's a good chance that whatever it is can be obtained through bhyve/bhyvectl/vmm or similar commands without needing to list then discard information about irrelevant processes (as the ps route does).
Respectfully, without wishing to detract from your initial full and complete answer, for which you have received thanks, I would point out that the initial post is an example of the common task of extracting a substring from a field in a record.
The alternatives demonstrate useful techniques that may be instructive to others. When I started at LQ, I learned a lot from reading such threads.
The thread is marked as solved, and unsure the value of all these variations? If efficiency is a goal then one should be going to the source of the information, and since it occurs in the "command" column, that's not likely to be ps?
It was answered by allend, just one more thing to add: we (a few lq members - or at least myself) just play with the issue and find my own way to solve. It is definitely not better and not against the previous answers, just a little bit different. Sometimes this additional solution can help (to OP) to understand, and sometimes we can see different approaches. (there were threads with solutions in more than 5 different languages).
OP will decide anyway...
Respectfully, without wishing to detract from your initial full and complete answer, for which you have received thanks
Please don't mistake my post for whatever that is attempting to address - I would have made (or attempted to make) the same point had post #2 come from someone else, but I guess I didn't word things too well.
Quote:
Originally Posted by allend
I would point out that the initial post is an example of the common task of extracting a substring from a field in a record.
The alternatives demonstrate useful techniques that may be instructive to others. When I started at LQ, I learned a lot from reading such threads.
Quote:
Originally Posted by pan64
we (a few lq members - or at least myself) just play with the issue and find my own way to solve.
I am aware of this - you will find me doing that often enough in the Programming forum - but (hopefully) always with something specific to demonstrate. In this thread I think post #4 is worth highlighting as a useful technique that some Awk users may be unaware of (or need reminding of).
It was when I caught myself about to post a variation of what allend wrote that I realised it was probably not a useful example.
When I wrote "there may be a more direct means to get at that value that doesn't need Awk at all" I was referring to either filtering ps directly (before I realised the relevant text was all in one column), or even better using a different command that outputs the relevant information.
And that's the point I was trying to make: there's likely a better solution via knowing what vm0:18 represents and determine how to get the information from the source.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.