• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

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
0
arvindhp
Asked:
arvindhp
  • 7
  • 6
  • 6
  • +2
1 Solution
 
griesshCommented:
Homework?

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

======
Werner
0
 
biraCommented:
Hi
   Try this

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

   
  This way you will always get the last field.


Regards
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
griesshCommented:
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
0
 
biraCommented:
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.


0
 
biraCommented:

  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


   

 
0
 
biraCommented:

  if the column is always the 4th ,

  try this:

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


0
 
biraCommented:

 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


0
 
ahoffmannCommented:
who am i|sed 's/[^ \t][^ \t]*[ \t][ \t]*[^ \t][^ \t]*[ \t][ \t]*[^ \t][^ \t]*[ \t][ \t]*\([^ \t]*\).*/\1/'
0
 
biraCommented:
who am i |read a b c d e f
echo $d
0
 
ahoffmannCommented:
doesn't work in csh, tcsh, etc.
0
 
arvindhpAuthor Commented:
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.

0
 
arvindhpAuthor Commented:
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.

0
 
arvindhpAuthor Commented:
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.

0
 
arvindhpAuthor Commented:
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.
0
 
arvindhpAuthor Commented:
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.
0
 
griesshCommented:
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
0
 
ahoffmannCommented:
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.
0
 
mnashadkaCommented:
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 }'
0
 
griesshCommented:
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
0
 
mnashadkaCommented:
Sorry, I missed that.
0
 
arvindhpAuthor Commented:
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
0
 
griesshCommented:
DONE

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 6
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now