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
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

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 .


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 06:27 AM.. Reason: code tags
Sponsored Links
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...

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

Sponsored Links
Old 01-09-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Join Date: Nov 2008
Last Activity: 27 February 2015, 2:22 AM EST
Location: Amsterdam
Posts: 9,948
Thanks: 321
Thanked 2,579 Times in 2,306 Posts

var=$(tail -1 infile)

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

Old 01-09-2013
Fundix Fundix is offline
Registered User
Join Date: Nov 2012
Last Activity: 27 February 2015, 1:33 AM EST
Posts: 77
Thanks: 20
Thanked 11 Times in 11 Posts
Input file :


command :

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

output :

Sponsored Links
Old 01-09-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
Join Date: Jan 2012
Last Activity: 24 February 2015, 2:15 PM EST
Location: Galactic Empire
Posts: 3,401
Thanks: 240
Thanked 1,215 Times in 1,141 Posts

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

Sponsored Links
Old 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...

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

Sponsored Links
Old 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:

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 06:09 AM
Retrieving values from the oracle table milink Shell Programming and Scripting 8 12-09-2010 07:09 AM
Retrieving values from tab-delimited file in unix script akashtcs Shell Programming and Scripting 4 08-27-2009 09:15 AM
Problem with retrieving values from properties file sailaja_80 Shell Programming and Scripting 2 08-21-2009 04:06 PM
Retrieving random numbers out of a text file nistleloy UNIX for Dummies Questions & Answers 13 12-14-2008 04:28 PM

All times are GMT -4. The time now is 03:29 AM.