Solved

how do I get the last field?

Posted on 2001-08-23
24
276 Views
Last Modified: 2010-04-21
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
Comment
Question by:arvindhp
  • 7
  • 6
  • 6
  • +2
24 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 6418838
Homework?

======
Werner
0
 
LVL 11

Expert Comment

by:griessh
ID: 6418849
If yes, here's a hint: do a man on 'read'.

======
Werner
0
 
LVL 6

Expert Comment

by:bira
ID: 6418867
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
 
LVL 11

Expert Comment

by:griessh
ID: 6418939
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
 
LVL 6

Expert Comment

by:bira
ID: 6418963
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
 
LVL 6

Expert Comment

by:bira
ID: 6418993

  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
 
LVL 6

Expert Comment

by:bira
ID: 6419027

  if the column is always the 4th ,

  try this:

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


0
 
LVL 6

Expert Comment

by:bira
ID: 6419253

 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
 
LVL 51

Accepted Solution

by:
ahoffmann earned 200 total points
ID: 6419534
who am i|sed 's/[^ \t][^ \t]*[ \t][ \t]*[^ \t][^ \t]*[ \t][ \t]*[^ \t][^ \t]*[ \t][ \t]*\([^ \t]*\).*/\1/'
0
 
LVL 6

Expert Comment

by:bira
ID: 6419634
who am i |read a b c d e f
echo $d
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6419709
doesn't work in csh, tcsh, etc.
0
 

Author Comment

by:arvindhp
ID: 6419895
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:arvindhp
ID: 6419916
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
 

Author Comment

by:arvindhp
ID: 6420020
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
 

Author Comment

by:arvindhp
ID: 6420062
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
 

Author Comment

by:arvindhp
ID: 6420111
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
 
LVL 11

Expert Comment

by:griessh
ID: 6420447
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6420785
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
 
LVL 8

Expert Comment

by:mnashadka
ID: 6420910
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
 
LVL 11

Expert Comment

by:griessh
ID: 6421730
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
 
LVL 8

Expert Comment

by:mnashadka
ID: 6422009
Sorry, I missed that.
0
 

Author Comment

by:arvindhp
ID: 6422722
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
 
LVL 11

Expert Comment

by:griessh
ID: 6422924
DONE

======
Werner
0
 

Author Comment

by:arvindhp
ID: 6424984
thanks a lot to everyone. This is really great, I found the solution for my problem.
-Aravind
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now