Retrieving values from a line in text file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Retrieving values from a line in text file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-09-2013
snowfrost snowfrost is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 14 January 2013, 5:25 AM EST
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Retrieving values from a line in text file

Hi,
I am new to Unix/ksh script and will like to check how do I retrieve just the count of '258' in the last line in a text file ?
There will be this "TRL" appended with number of count at the last line in a text file .


Code:
TRL0000000258



Code:
var=`grep 'TRL' $HOME/folder/test.txt | wc -l`

How do i substr and get the last 10 values into a variable ?

Appreicate anyone who can help me . Thank You.

Last edited by Scrutinizer; 01-09-2013 at 05:27 AM.. Reason: code tags
Sponsored Links
    #2  
Old 01-09-2013
bmk bmk is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 31 October 2013, 9:16 AM EDT
Posts: 262
Thanks: 1
Thanked 22 Times in 22 Posts
Can you post the input file?

---------- Post updated at 05:54 AM ---------- Previous update was at 05:38 AM ----------

I think you are excepting this one...

Code:
 echo "TRL44440000000258"|awk '{ print substr( $1, length($1) - 9, length($1) ) }'

Sponsored Links
    #3  
Old 01-09-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 30 September 2014, 7:43 AM EDT
Location: Amsterdam
Posts: 9,487
Thanks: 280
Thanked 2,398 Times in 2,150 Posts
Try:


Code:
var=$(tail -1 infile)
var=${var#TRL}


Code:
var=$(sed -n '$s/TRL//p' infile)

    #4  
Old 01-09-2013
Fundix Fundix is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 16 January 2014, 5:23 AM EST
Posts: 71
Thanks: 16
Thanked 11 Times in 11 Posts
Input file :

Code:
AAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCC
TRL0000000258

command :

Code:
awk ' /^TRL/ {sub(/TRL/, "") ; print } ' file

output :
Code:
0000000258

Sponsored Links
    #5  
Old 01-09-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 26 September 2014, 6:37 PM EDT
Location: Galactic Empire
Posts: 3,385
Thanks: 234
Thanked 1,208 Times in 1,134 Posts

Code:
awk '/^TRL/{sub(/TRL0+/,"")}1' test.txt

Sponsored Links
    #6  
Old 01-09-2013
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 29 September 2014, 3:17 PM EDT
Location: USA
Posts: 1,472
Thanks: 19
Thanked 144 Times in 136 Posts
Yet another way...

Code:
echo TRL0000000258 | awk '{gsub("[^0-9]", "");print $0+0}'

Sponsored Links
    #7  
Old 01-09-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 23 September 2014, 12:09 PM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts
try also (using Scrutinizer's sed example) for eliminating leading non valid chars for numbers:

Code:
var=$(sed -n '$s/^[^1-9]*//p' infile)

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Converting filenames from julian day to yyyy-mm-dd and retrieving weekly mean values ida1215 Shell Programming and Scripting 3 02-14-2012 05:09 AM
Retrieving values from the oracle table milink Shell Programming and Scripting 8 12-09-2010 06:09 AM
Retrieving values from tab-delimited file in unix script akashtcs Shell Programming and Scripting 4 08-27-2009 08:15 AM
Problem with retrieving values from properties file sailaja_80 Shell Programming and Scripting 2 08-21-2009 03:06 PM
Retrieving random numbers out of a text file nistleloy UNIX for Dummies Questions & Answers 13 12-14-2008 03:28 PM



All times are GMT -4. The time now is 07:50 AM.