LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 08-31-2022, 02:08 PM   #1
marietto2008
LQ Newbie
 
Registered: May 2020
Posts: 23

Rep: Reputation: Disabled
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 :


Code:
# 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 :


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

it gives :

Code:
vm0:18
/bhyve:

when I do :


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

it gives :


Code:
vm0:18

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

thanks.
 
Old 08-31-2022, 02:43 PM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,633

Rep: Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558
Quote:
Originally Posted by marietto2008 View Post
but they are both wrong. The result should be : 0:18
They are both correct, because they are giving what the code is asking for.

Awk's filter pattern does not affect the value of fields, and all the commands are asking it to do is print $6 without making any changes to it.

There are various ways to get 0:18 - which one is appropriate depends on how representative the example data is, but a couple of examples...

Strip vm from the start of column 6 with:
Code:
awk '/bhyve: +vm/{sub(/^vm/,"",$6);print $6}'
Or perhaps better to replace everything not a digit or colon:
Code:
awk '/bhyve: +vm/{gsub(/[^0-9:]/,"",$6);print $6}'

(Also, depending on what "vm0:18" actually is, there may be a more direct means to get at that value that doesn't need Awk at all.)

 
1 members found this post helpful.
Old 08-31-2022, 02:59 PM   #3
marietto2008
LQ Newbie
 
Registered: May 2020
Posts: 23

Original Poster
Rep: Reputation: Disabled
both work good. very thanks.

Code:
# ps ax | awk '/bhyve: +vm/{sub(/^vm/,"",$6);print $6}'
0:9

# ps ax | awk '/bhyve: +vm/{gsub(/[^0-9:]/,"",$6);print $6}'
0:9
 
Old 08-31-2022, 09:51 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,836

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
Using the exact fields avoids to match itself in the ps list.
And sub() gives an exit status.
Code:
ps ax | awk '$5=="bhyve:" && sub(/^vm/,"",$6) {print $6}'
 
Old 09-01-2022, 12:10 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,367
Blog Entries: 3

Rep: Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771Reputation: 3771
Another option might be to use ps and pgrep for pre-processing.

Code:
ps -p $(pgrep -x bhyve) -o args

ps -p $(pgrep -x bhyve) -o args | awk ...
However, which distro, or OS even, is this for? The behavior of ps depends on which one you have.
 
Old 09-01-2022, 12:32 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,066

Rep: Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363
Using ps and pgrep together looks overkill for me (including awk)
Code:
ps ax | awk -F'[m ]+' '/bhyve: vm/ { print $7 }'
 
Old 09-01-2022, 09:49 AM   #7
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,383

Rep: Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764
Just for fun, leaving the awk hammer in the toolbox.
Code:
ps ax | grep -o "vm[^ ]*" | tr -d "vm"

Last edited by allend; 09-01-2022 at 10:02 AM.
 
Old 09-01-2022, 10:56 AM   #8
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,633

Rep: Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558

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).

 
Old 09-01-2022, 06:49 PM   #9
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,383

Rep: Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764
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.
 
Old 09-02-2022, 12:16 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,066

Rep: Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363
Quote:
Originally Posted by boughtonp View Post
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...
 
Old 09-02-2022, 07:00 AM   #11
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,633

Rep: Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558
Quote:
Originally Posted by allend View Post
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 View Post
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.

 
  


Reply

Tags
awk, scripting, shell scripting, shell scripts, ubuntu



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed inside awk or awk inside awk maddyfreaks Linux - Newbie 4 06-29-2016 01:10 PM
Exact string match in multi line string Arun Shankar Programming 2 06-19-2014 09:54 AM
[SOLVED] Once again... awk.. awk... awk shivaa Linux - Newbie 13 12-31-2012 04:56 AM
Awk: match string in exact position sebelk Programming 2 10-19-2009 02:15 PM
If possible, could anyone give me exact instructions to dual-boot WinXP and Slackware crumb Linux - General 5 10-16-2003 10:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration