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
# 8  
Old 01-14-2013
Hi Fundix,

Input File :
Code:
AAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCC
TRL0000000258

When I try to code using
Code:
awk ' /^TRL/ {sub(/TRL/, "") ; print } ' $src_file

, it will prompt me for the below error :
Code:
awk:syntax error near line 1
awk:illegal statement near line 1.

Thanks everyone for your help !

Moderator's Comments:
Mod Comment Please use code tags for code and data

Last edited by Scrutinizer; 01-14-2013 at 05:40 AM.. Reason: error; mod: code tags
# 9  
Old 01-14-2013
On Solaris use /usr/xpg4/bin/awk rather than awk
# 10  
Old 01-14-2013
Hi Scrutinizer ,
Thanks .
Is it possible for me to eliminate leading non valid chars for numbers?

Example :
Code:
0000000002

To get 2 as the variable instead.

Thanks
# 11  
Old 01-14-2013
Hi, try forcing a numerical context by using print $0+0 instead of print (which is equivalent to print $0 ), thus losing the leading zeroes
Code:
awk ' /^TRL/ {sub(/TRL/, "") ; print $0+0 } ' file

Or remove the leading zeroes as part of the substitution:
Code:
awk 'sub(/^TRL0*/,x)' file

Code:
sed -n '$s/^TRL0*//p' file

# 12  
Old 01-14-2013
Hi Scrutinizer,
Is it possible for me to assign the value to a variable instead of just printing out on screen ?

Code:
var=awk 'sub(/^TRL0*/,x)' file
echo $var

Sorry. I am not that familar with using awk and ksh unix. Thank You.
# 13  
Old 01-14-2013
Use:
Code:
var=$(awk 'sub(/^TRL0*/,x)' file)

This User Gave Thanks to Scrutinizer For This Post:
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