Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
04-05-2006, 02:14 AM
|
#1
|
Member
Registered: Dec 2005
Posts: 48
Rep:
|
how to select a line using awk
Hi,
I wish to know how to select a line using awk.
when I do "`ps -ax | grep "pattern" | awk {'print $5'}`"
it gives me following output
/pattern
/pattern
/pattern
/pattern
/pattern
I want to select first line of above output for some
comparision.
Please help me to solve the above problem.
|
|
|
04-05-2006, 02:29 AM
|
#2
|
Senior Member
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524
Rep:
|
have a look at `head' (-n 1)
|
|
|
04-05-2006, 02:40 AM
|
#3
|
LQ Guru
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870
|
Quote:
Originally Posted by sharad
Hi,
I wish to know how to select a line using awk.
when I do "`ps -ax | grep "pattern" | awk {'print $5'}`"
it gives me following output
/pattern
/pattern
/pattern
/pattern
/pattern
I want to select first line of above output for some
comparision.
Please help me to solve the above problem.
|
a quick and dirty way to get just the first line of the output would be with "head", like so:
Code:
ps ax | grep "pattern" | awk '{ print $5 }' | head -n 1
EDIT: sorry, just realized that a reply had already been posted... sometimes i forget to refresh the tabs before posting...
Last edited by win32sux; 04-05-2006 at 04:03 AM.
|
|
|
04-05-2006, 07:35 AM
|
#4
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
Or just for fun if you just wanna use awk:
Code:
ps ax | awk '/pattern/ {if (p == "") {p = $5}} END {print p}'
jlinkels
Last edited by jlinkels; 04-05-2006 at 07:36 AM.
|
|
|
04-05-2006, 07:55 AM
|
#5
|
Senior Member
Registered: Sep 2005
Location: Out
Posts: 3,307
Rep:
|
Another one
Code:
ps auxwwf | awk '/pattern/ {print $5; exit;}'
|
|
|
04-05-2006, 09:26 AM
|
#6
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
Darn nx5000, yours is more elegant than mine.
But mine is less comprehensible
jlinkels
|
|
|
All times are GMT -5. The time now is 12:21 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|