LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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
  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: 47
have a look at `head' (-n 1)
 
Old 04-05-2006, 02:40 AM   #3
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
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
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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: 57
Another one
Code:
 ps auxwwf | awk '/pattern/ {print $5; exit;}'
 
Old 04-05-2006, 09:26 AM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

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

But mine is less comprehensible

jlinkels
 
  


Reply



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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:03 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