Retrieving values from a line in text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Retrieving values from a line in text file
# 1  
Old 01-09-2013
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 06:27 AM.. Reason: code tags
# 2  
Old 01-09-2013
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) ) }'

# 3  
Old 01-09-2013
Try:

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

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

# 4  
Old 01-09-2013
Input file :
Code:
AAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCC
TRL0000000258

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

output :
Code:
0000000258

# 5  
Old 01-09-2013
Code:
awk '/^TRL/{sub(/TRL0+/,"")}1' test.txt

# 6  
Old 01-09-2013
Yet another way...
Code:
echo TRL0000000258 | awk '{gsub("[^0-9]", "");print $0+0}'

# 7  
Old 01-09-2013
try also (using Scrutinizer's sed example) for eliminating leading non valid chars for numbers:
Code:
var=$(sed -n '$s/^[^1-9]*//p' infile)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print line if values in fields matches number and text

datafile: 2017-03-24 10:26:22.098566|5|'No Route for Sndr:RETEK RMS 00040 /ZZ Appl:PF Func:PD Txn:832 Group Cntr:None ISA CntlNr:None Ver:003050 '|'2'|'PFI'|'-'|'EAI_ED_DeleteAll'|'EAI_ED'|NULL|NULL|NULL|139050594|ActivityLog| 2017-03-27 02:50:02.028706|5|'No Route for... (7 Replies)
Discussion started by: SkySmart
7 Replies

2. Shell Programming and Scripting

Extracting values based on line-column numbers from multiple text files

Dear All, I have to solve the following problems with multiple tab-separated text file but I don't know how. Any help would be greatly appreciated. I have access to Linux mint (but not as a professional). I have multiple tab-delimited files with the following structure: file1: 1 44 2 ... (5 Replies)
Discussion started by: Bastami
5 Replies

3. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

4. Shell Programming and Scripting

Retrieving line number from grep

Hi. im trying to retrieve the line number from grep. i have 1 part of my code here. grep -n $tgt file.txt | cut -f 1 -d ":" when i do not cut the value i have is 12:aaa:abc:aaa:aaa:aaa how can i store the value of 12 or my whole line of string into a variable with grep? (6 Replies)
Discussion started by: One_2_three
6 Replies

5. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

Converting filenames from julian day to yyyy-mm-dd and retrieving weekly mean values

Hi, I need help to convert the filenames of my 9-year daily files (1999-2007) from a julian day to yyyy-mm-dd format. my original files are patterned likes the ones below. 1999001.txt 1999002.txt 1999003.txt 1999004.txt ... 1999365.txt desired output: 19990101.txt 19990102.txt... (3 Replies)
Discussion started by: ida1215
3 Replies

7. Shell Programming and Scripting

Retrieving values from the oracle table

Hi, How to retrieve two different date values(min & max) from the oracle table and assign to two different variables in the shell script to process further. With Regards (8 Replies)
Discussion started by: milink
8 Replies

8. Shell Programming and Scripting

Retrieving values from tab-delimited file in unix script

Hi I am trying to retrieve values from a tab-delimited file.I am using while read record value=`echo $record | cut -f12` done Where 12 is the column no i want retieve and record is one line of the file. But it is returning the full record. Plz help (4 Replies)
Discussion started by: akashtcs
4 Replies

9. Shell Programming and Scripting

Problem with retrieving values from properties file

I have an input file like RMS_RPT_PERIOD_DIM,Table,NYTD_SLS_DM,GPS_SLS_DM1,NYTD_SLS_GPS_INT,RMS_DM,byreddys,7/31/2009,byreddys,7/31/2009,Y,//depot/eqr/salesgps/trunk/src/db/table,TBL_GPS_CONTACT_DETAILS.sql,1.1,,lakshmi,sql,y... (2 Replies)
Discussion started by: sailaja_80
2 Replies

10. UNIX for Dummies Questions & Answers

Retrieving random numbers out of a text file

Hi one and all, I'm working on a Bash script that is designed to calculate how much IP traffic has passed through a port to determine traffic volume over a given amount of time. I've currently been able to use the netstat -s command coupled with grep to write to a file the total packets... (13 Replies)
Discussion started by: nistleloy
13 Replies
Login or Register to Ask a Question