Awk - Using a Shell Variable in the Reg Expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk - Using a Shell Variable in the Reg Expression
# 1  
Old 11-01-2006
Awk - Using a Shell Variable in the Reg Expression

Hi all,

I have a shell variable $test1 that holds a value derived from some other processing. What I need to do is use that $test1 as the input to a awk regular expression:

nawk -F"," -v tester=$test1 '
/tester/{
print $0
}
' $inputFile

So what I have is tester which def has a value. I want to search in the $inputFile for the string in tester but Im presuming /tester/ will look for the literal string 'tester' rather then whats contained within it which is the desired string,

Can someone help out with this,

Thanks,
# 2  
Old 11-01-2006
Code:
nawk -F',' -v tester=$test1 '
   $0 ~ tester { print $0 }' $inputFile

# 3  
Old 11-01-2006
Hi thanks for the quick response,

After adding the code suggested I am getting a response but it seems to be returning more than I expected. The value of tester is "qwerty1" and in the
$inputFile there is only 1 line with this value but the code is returning all the lines from the input...

Any further suggestions?
# 4  
Old 11-01-2006
post a snippet from the '$inputFile' and the value of '$tester'
# 5  
Old 11-01-2006
Hi,

tester contains: COMM_VOL.TEST
and the sample input file:

,qwerty,qwerty,None,True,0,% ANNU actual/actual,0,False,@Constant,,,:,False,@Linear(),2006/1
0/06,Option Time,
,,,,,,,,,,,,,,,,2006/11/16,0.28
,,,,,,,,,,,,,,,,2006/12/16,0.28
,COMM_VOL.TEST1,COMM_VOL.TEST1,None,True,0,% ANNU actual/actual,0,False,@Constant,,,:,False,@Linear(),2006/1
0/06,Option Time,
,,,,,,,,,,,,,,,,2006/11/16,0.28
,,,,,,,,,,,,,,,,2006/12/16,0.28
,COMM_VOL.TEST2,COMM_VOL.TEST2,None,True,0,% ANNU actual/actual,0,False,@Constant,,,:,False,@Linear(),2006/1
0/06,Option Time,
,,,,,,,,,,,,,,,,2006/11/16,0.28
,,,,,,,,,,,,,,,,2006/12/16,0.28

Therfore I would expect tester to match just the lines in bold & italics but the whole lot is coming back...even the lines with the "," and the line with qwerty in it,
# 6  
Old 11-01-2006
not4,

Here is a ksh subroutine that works for me. The ksh variables for Fs, Col_numb and Pattern must be set before calling the subroutine.

Code:
print_line_containing_pattern () {
cat $Infile | $Awk -F"$Fs" -v col=$Col_numb -v pat="$Pattern" '$(col) ~ pat {print $0}'
}
}

My Awk variable is set to

Code:
#Awk=/usr/bin/gawk                      # cygwin
Awk="/usr/xpg4/bin/awk"                 # posix (sol)
#Awk="/usr/bin/awk"                     # old awk (sol)
#Awk="/usr/bin/nawk"                    # new awk (sol)

Good Luck !
KW

Last edited by kwachtler; 11-02-2006 at 02:28 PM..
# 7  
Old 11-01-2006
Heya,
works just fine for me under Solaris. Try this:
Code:
nawk -F',' -v tester="$test1" '$0 ~ tester { print $0 }' $inputFile

Note: next time you post either your code samples and/or text file samples, pls do use vB Codes - it makes it easier to read and does convey the unformated text.

Last edited by vgersh99; 11-01-2006 at 10:50 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

REG Expression

Need your help in creating regular expression for particular set. let say I have given two dates 20130623 to 20140625. I need to create regular for the dates which fall in between above two dates. (4 Replies)
Discussion started by: gvkumar25
4 Replies

2. UNIX for Dummies Questions & Answers

Initialize Variable Using awk Expression

Hello All, What i am trying to do is read the input file delimited by pipe and do further logic like simulating the for loop but using awk not sure if this a right approach but as i was told doing "for i in `cat $file`" is an over kill so trying to replicate the same thing using awk script below.... (6 Replies)
Discussion started by: Ariean
6 Replies

3. Shell Programming and Scripting

reg expression in perl

how to uniquely match each of the words seperated by / in perl ${REP_PATH}/FUNCTIONAL/wide1c_1.0V/max/qor.rpt https://www.unix.com/images/misc/progress.gif (5 Replies)
Discussion started by: dll_fpga
5 Replies

4. Shell Programming and Scripting

Reg expression

Please let me understand this reg expression (\s+')(.*)('\s+)(.)(.*)(\/.*)/) i have doubt in the below 2.I'm not understanding why back-tick used? (\s+') and ('\s+) (2 Replies)
Discussion started by: dll_fpga
2 Replies

5. Shell Programming and Scripting

awk reg expression

Hello, I have thousand of messages (HL7), I want to use awk to extract only the ones that have a particular value in pv1.18 Each record in the file is the whole HL7 message, ie. when I print $0 I get the whole message MSH EVN PID etc. ,there is an x0d between the segments. I would like to use a... (3 Replies)
Discussion started by: gio001
3 Replies

6. Shell Programming and Scripting

Awk's variable in regular expression

Anyone know how I will use awk's variable in a regular expression? This line of code of mine is working, the value PREMS should be a variable: awk '$1 ~ /PREMS/ { if(length(appldata)+2 >= length($1)) print $0; }' appldata=$APPLDATA /tmp/file.tmp The value of APPLDATA variable is PREMS. ... (2 Replies)
Discussion started by: Orbix
2 Replies

7. Shell Programming and Scripting

usage...sed/awk/reg-exp ..in shell scripting

in shell scripting there is extensive usage of i> regular expression ii>sed iii>awk can anyone tell me the suitable contexts ...i mean which one is suitable for what kind of operation. like the reg-exp and sed seems to be doing the same job..i.e pattern matching (1 Reply)
Discussion started by: mobydick
1 Replies

8. Shell Programming and Scripting

Reg expression For

HI system.sysUpTime.0 : Timeticks: (1519411311) 175 days, 20:35:13.11 From the above output i need only 175days in a perl script.. Please Help (2 Replies)
Discussion started by: Harikrishna
2 Replies

9. UNIX for Dummies Questions & Answers

Help with Reg. Expression

I need help with this: Can any one tell me what does these below mean: 1. "\(.\).*") != '/' 2. sed 's+^\./++;s+/.*++' 3. sed "s+${f}/+ + Thanks in advance (7 Replies)
Discussion started by: moe2266
7 Replies

10. Shell Programming and Scripting

Using reg. exp. variable in AWK??

Any idea please: How to pass a reg. exp. variable to awk call in a shell??? Thank u #!/bin/sh reg_exp=name awk '/reg_exp/{ print; }' $1 (5 Replies)
Discussion started by: andy2000
5 Replies
Login or Register to Ask a Question