The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
getopts alternative? krishmaths Shell Programming and Scripting 3 04-05-2008 10:43 PM
Alternative to date +%s Data469 HP-UX 6 03-31-2008 12:28 PM
Using awk (or an alternative) michaeltravisuk Shell Programming and Scripting 5 03-08-2007 08:37 PM
.NET Alternative goon12 UNIX for Dummies Questions & Answers 3 04-06-2005 09:07 AM
loop alternative? apalex Shell Programming and Scripting 2 05-02-2002 09:47 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 03-01-2005
Registered User
 

Join Date: Feb 2005
Posts: 71
Question cut -f (or awk alternative)

I'm trying the following as i wish to get the last field in a report containing several fields, each line can have more fields than the previous, so I tried

rvwords=`echo $rvstatus |wc -w`
rvstat=`echo $rvstatus |cut -f $rvwords`

but it prints the whole variable contents of $rvstatus rather than just the seventh field, there are no delimeters (other than spaces / tabs) in the line, which would read along the lines of

0 1111 my file 102.00 1003.00 2.99
1 2222 my own file 111.00 112.98 200.10
...

Thanks in anticipation
Reply With Quote
Forum Sponsor
  #2  
Old 03-01-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,003
rvstat=`echo $rvstatus | nawk '{print $NF}'`

or with sed:

rvstat=`echo $rvstatus | sed -e 's/.* \([^ ][^ ]*\)[ ]*$/\1/'`

Last edited by vgersh99; 03-01-2005 at 11:39 AM.
Reply With Quote
  #3  
Old 03-01-2005
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
Try this ....

Code:
$ echo "0 1111 my file 102.00 1003.00 2.99" | cut -d" " -f 7
2.99
Try this if we have more than a one embedded space between the fields

Code:
$ echo "0 1111 my file 102.00 1003.00 2.99" | tr -s " " | cut -d" " -f 7
2.99
Reply With Quote
  #4  
Old 03-02-2005
Registered User
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
Try as with awk as,

Code:
awk ' { print $NF }' <filename>

or 

sed 's/.*[ ]\(.*\)$/\1/' <filename>
IT will do too.
Reply With Quote
  #5  
Old 03-02-2005
Registered User
 

Join Date: Feb 2005
Posts: 71
Quote:
Originally Posted by vgersh99
rvstat=`echo $rvstatus | nawk '{print $NF}'`

or with sed:

rvstat=`echo $rvstatus | sed -e 's/.* \([^ ][^ ]*\)[ ]*$/\1/'`
Thanks the awk method works fine, however this now raises another issue, how about if I now wanted to print the field fourth and/or fifth from the right ?
Reply With Quote
  #6  
Old 03-02-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,003
Quote:
Originally Posted by gefa
Thanks the awk method works fine, however this now raises another issue, how about if I now wanted to print the field fourth and/or fifth from the right ?
Code:
# 4th AND 5th from last
rvstat=`echo $rvstatus | nawk '{print $(NF-4), $(NF-5)}'`

# 4th from last
rvstat=`echo $rvstatus | nawk '{print $(NF-4)}'`
Reply With Quote
  #7  
Old 03-02-2005
Registered User
 

Join Date: Feb 2005
Posts: 71
Quote:
Originally Posted by vgersh99
Code:
# 4th AND 5th from last
rvstat=`echo $rvstatus | nawk '{print $(NF-4), $(NF-5)}'`

# 4th from last
rvstat=`echo $rvstatus | nawk '{print $(NF-4)}'`
Thanks again, that works as required, I had tried something similar but missed off the brackets !.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0