LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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

Reply
 
LinkBack Search this Thread
Old 04-05-2006, 02:14 AM   #1
sharad
Member
 
Registered: Dec 2005
Posts: 48

Rep: Reputation: 15
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.
 
Old 04-05-2006, 02:29 AM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
have a look at `head' (-n 1)
 
Old 04-05-2006, 02:40 AM   #3
win32sux
Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
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.
 
Old 04-05-2006, 07:35 AM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 3,977

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
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.
 
Old 04-05-2006, 07:55 AM   #5
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
Another one
Code:
 ps auxwwf | awk '/pattern/ {print $5; exit;}'
 
Old 04-05-2006, 09:26 AM   #6
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 3,977

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
Darn nx5000, yours is more elegant than mine.

But mine is less comprehensible

jlinkels
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Awk - get a parameter from the command line benjalien Programming 1 01-24-2006 09:06 AM
Select() did not select my socket thvo Programming 1 05-08-2005 12:20 AM
Awk command-line arguments lowpro2k3 Programming 1 03-28-2005 09:09 PM
Deleting a line with gawk/awk caps_phisto Linux - General 4 11-06-2004 02:31 PM
I there a way to tell awk to select rows instead of collums jsandro7 Linux - Newbie 2 06-14-2004 10:58 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration