The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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-06-2008 01:43 AM
Alternative to date +%s Data469 HP-UX 6 03-31-2008 03:28 PM
Using awk (or an alternative) michaeltravisuk Shell Programming and Scripting 5 03-08-2007 11:37 PM
.NET Alternative goon12 UNIX for Dummies Questions & Answers 3 04-06-2005 12:07 PM
loop alternative? apalex Shell Programming and Scripting 2 05-02-2002 12:47 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-01-2005
gefa gefa is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 72
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
  #2 (permalink)  
Old 03-01-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

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

or with sed:

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

Last edited by vgersh99; 03-01-2005 at 02:39 PM..
  #3 (permalink)  
Old 03-01-2005
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
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
  #4 (permalink)  
Old 03-02-2005
muthukumar muthukumar is offline
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.
  #5 (permalink)  
Old 03-02-2005
gefa gefa is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 72
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 ?
  #6 (permalink)  
Old 03-02-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
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)}'`
  #7 (permalink)  
Old 03-02-2005
gefa gefa is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 72
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 !.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0