Solved

how do I get the last field?

Posted on 2001-08-23
24
265 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

762 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

19 Experts available now in Live!

Get 1:1 Help Now