Link to home
Start Free TrialLog in
Avatar of arvindhp
arvindhp

asked on

how do I get the last field?

Hi,
  I am having requirement to access the the last field of an output of a command. For example:
I want to access the last field in 'who am i' command output.
  I know the best way to do this is by 'awk' say
$n=`who am i|awk '{printf $4}'`
$echo $n
  But the requirement is such that awk should not be used. Also I tried using the 'cut' command as follows
$n=`who am i|cut -f7 -d' '`
$echo $n
  Though it seems to work, the position i.e. the -f argument is not the same everytime in my original problem.
  I want a very simple solution similar to awk, i.e. neither it should not be output to some file and get from there nor use perl script because I don't have one.
  The column that I am trying to access is always at the 4th parameter.

  Can anybody help?

regards
Aravinda
Avatar of griessh
griessh
Flag of United States of America image

Homework?

======
Werner
If yes, here's a hint: do a man on 'read'.

======
Werner
Avatar of bira
bira

Hi
   Try this

  s="who am i"
  echo $s | rev |cut -f1 -d' '|rev

   
  This way you will always get the last field.


Regards
bira

I think you started something here. I guess the "who am i" has different formats on different systems.
My AIX gives me:
griessh pts/1 Aug 09 14:00

That is 5 fields. If arvindhp really needs the LAST field, your suggestion works. If he needs the 4th field of 5 ...

BTW: This question really smells like homework (UNIX commands for beginners).

======
Werner
The "who am i" i have posted was only an example of string.
  Not the command whoami.

   see

    s="This is an example"
    echo $s|rev|cut -f1 -d' '|rev

     will display "example" , that is the last field.



  Other example:

     the command uname -a would display something like
 AIX RISC 1 4 00000216C000

s=`uname -a`
  echo $s|rev|cut -f1 -d' '|rev

  will display only 00000216C000


   

 

  if the column is always the 4th ,

  try this:

   s="this is a test"
   echo $s |read a b c d
   echo $d



 If there are more columns beyond the 4th :
s="this is a test"
  echo $s |read a b c d e f g h
  echo $d


ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
who am i |read a b c d e f
echo $d
doesn't work in csh, tcsh, etc.
Avatar of arvindhp

ASKER

Thanks a lot for all Unix Gurus. Yeah it looks like home work as griessh says.
The sed command seems to be fine for me, as 'rev' and 'read' were unable to solve my original problem. But 'ahoffmann' can you explain to me what this strange looking command, so that I can modify this to my requirement. Yeah I am very poor in unix.

Thanks a lot for all Unix Gurus. Yeah it looks like home work as griessh says.
The sed command seems to be fine for me, as 'rev' and 'read' were unable to solve my original problem. But 'ahoffmann' can you explain to me what this strange looking command, so that I can modify this to my requirement. Yeah I am very poor in unix.

Thanks a lot for all Unix Gurus. Yeah it looks like home work as griessh says.
The sed command seems to be fine for me, as 'rev' and 'read' were unable to solve my original problem. But 'ahoffmann' can you explain to me what this strange looking command, so that I can modify this to my requirement. Yeah I am very poor in unix.

there seems to be some problem in site, I am unable to see the comments added by griessh and bira. Instead of this comments I am seeing my own update. So I am seeing 3 copies ( 1 original mine and 2 griessh and bira ). Is this happening to everyone,so that I'll escalate to helpdesk.
there seems to be some problem in site, I am unable to see the comments added by griessh and bira. Instead of this comments I am seeing my own update. So I am seeing 3 copies ( 1 original mine and 2 griessh and bira ). Is this happening to everyone,so that I'll escalate to helpdesk.
Folks

>>Yeah it looks like home work as griessh says.

I tried to stop it, but you were to busy finding new combinations ... You know we shouldn't do that ...

======
Werner
homework (I'll not answer) or not, but this one is to simple (-:except the sed command ;-)

arvindhp,
the simlest an most human readable solution is with awk. Dot.
A more perfect and flexibale would be with perl.
Then there is to clarify if you want to have the last (as asked in your question) or the 4th (as written in a comment later) field. And everyone assumed that fields are separated by white space (you didn't tell us).
My solution works as long as whitespaces are blank or tab, and as long as there are at least 4 fileds, then the 4'th field is printed.
I think that you should look at using awk as ahoffman mentioned:
# For the last field:
who am i | awk '{ print $NF }'

# For the 4th field:
who am i | awk '{ print $4 }'
Even if it's simple for you, it's not simple for him (that's why he has to learn it). And awk is according to his question not allowed ... Exclamation mark :-)

======
Werner
Sorry, I missed that.
can any one mail me the update by griessh and bira at posted at Date: 08/23/2001 02:34PM PST and Date: 08/23/2001 03:09PM PST or all the update to my mail address "arvindhp@yahoo.com". I have a problem seeing these two updates which is very important to me.

-Aravind
DONE

======
Werner
thanks a lot to everyone. This is really great, I found the solution for my problem.
-Aravind