1838577 Members
4037 Online
110128 Solutions
New Discussion

About the shell script

 
SOLVED
Go to solution
peterchu
Super Advisor

About the shell script

In the shell ,
#who -u |grep testuser
testuser pts/17 Jan 1 13:55 00:50 16525 (168.0.7.1)
testuser pts/16 Jan 1 09:25 00:15 20834 (198.0.7.2)

#who -u |grep testuser |awk '{ print $7 }
16525
20834

If I only want to get the result of "16525" ( get the line 1 only ) , what can I do ? thx
9 REPLIES 9
Biswajit Tripathy
Honored Contributor

Re: About the shell script

#who -u |grep testuser |awk '{ print $7 } | xargs echo | awk '{print $1}'

- Biswajit
:-)
Trond Haugen
Honored Contributor

Re: About the shell script

#who -u |grep testuser |awk '{ print $7 }|head -1

Regards,
Trond
Regards,
Trond Haugen
LinkedIn
Biswajit Tripathy
Honored Contributor

Re: About the shell script

peterchu,
Trond's reply is probably more efficient than my previous reply.

Another way to do this is:

#who -u |grep testuser |awk '{ print $7 } | read -r a; echo $a

- Biswajit
:-)
Biswajit Tripathy
Honored Contributor

Re: About the shell script

...and, ofcourse..
#who -u |grep testuser | read -r a b c d e f g h;echo $g

:-)

- Biswajit
:-)
Jannik
Honored Contributor
Solution

Re: About the shell script

and even better:
who -u | awk '/testuser/ {print $7}' | head -1

Best Regards!
jaton
Jose Mosquera
Honored Contributor

Re: About the shell script

Hi,

who -u|grep ^testuser|awk '{print $7}'|head -1

BR.
Victor Fridyev
Honored Contributor

Re: About the shell script

Hi,

who -u |grep testuser |awk '{print $7;exit}'

HTH
Entities are not to be multiplied beyond necessity - RTFM
Leif Halvarsson_2
Honored Contributor

Re: About the shell script

Hi,

Perhaps the most correct solution if you want to use awk

who -u |grep testuser |awk 'BEGIN {getline; print $7 }'

Tim D Fulford
Honored Contributor

Re: About the shell script

And the perl entry is...

who -u | perl -ane 'if (m/testuser/) {print "$F[6]\n"}' | head -1

I'm sure perl has a internal "head" type function but I'm not 100% sure!!

Tim
-