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

  Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Retrieving values from a line in text file

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 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 Unix and Linux 01-09-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 April 2015, 11:25 PM EDT
Location: Amsterdam
Posts: 10,066
Thanks: 324
Thanked 2,633 Times in 2,354 Posts
Try:


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


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

    #4  
Old Unix and Linux 01-09-2013
Fundix Fundix is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 10 March 2015, 11:42 AM EDT
Posts: 78
Thanks: 23
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 Unix and Linux 01-09-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 1 April 2015, 5:28 PM EDT
Location: Galactic Empire
Posts: 3,401
Thanks: 240
Thanked 1,215 Times in 1,141 Posts

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

Sponsored Links
    #6  
Old Unix and Linux 01-09-2013
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 14 January 2015, 3:24 PM EST
Location: USA
Posts: 1,555
Thanks: 25
Thanked 150 Times in 142 Posts
Yet another way...

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

Sponsored Links
    #7  
Old Unix and Linux 01-09-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 13 January 2015, 10:34 AM EST
Location: Houston, Texas, USA
Posts: 681
Thanks: 0
Thanked 206 Times in 198 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 02:31 AM.