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


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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-09-2013
Registered User
 
Join Date: Jan 2007
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
Posts: 258
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
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,808
Thanks: 147
Thanked 1,771 Times in 1,608 Posts
Try:


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


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

    #4  
Old 01-09-2013
Registered User
 
Join Date: Nov 2012
Posts: 58
Thanks: 15
Thanked 9 Times in 9 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
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,510
Thanks: 167
Thanked 816 Times in 785 Posts

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

Sponsored Links
    #6  
Old 01-09-2013
Registered User
 
Join Date: Oct 2007
Location: USA
Posts: 1,328
Thanks: 13
Thanked 108 Times in 103 Posts
Yet another way...

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

Sponsored Links
    #7  
Old 01-09-2013
Registered User
 
Join Date: Sep 2012
Location: Houston, Texas, USA
Posts: 571
Thanks: 0
Thanked 174 Times in 168 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
Reply

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 03:40 PM.