LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   how to select a line using awk (https://www.linuxquestions.org/questions/linux-software-2/how-to-select-a-line-using-awk-431860/)

sharad 04-05-2006 02:14 AM

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.

jonaskoelker 04-05-2006 02:29 AM

have a look at `head' (-n 1)

win32sux 04-05-2006 02:40 AM

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

jlinkels 04-05-2006 07:35 AM

Or just for fun if you just wanna use awk:

Code:

  ps ax | awk '/pattern/ {if (p == "") {p = $5}} END {print p}'
jlinkels

nx5000 04-05-2006 07:55 AM

Another one
Code:

ps auxwwf | awk '/pattern/ {print $5; exit;}'

jlinkels 04-05-2006 09:26 AM

Darn nx5000, yours is more elegant than mine.

But mine is less comprehensible :)

jlinkels


All times are GMT -5. The time now is 11:05 AM.