The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare variable against regular expression? finalight Shell Programming and Scripting 6 05-22-2008 07:20 AM
how to use regular expression in Bash Shell Scripting sunitachoudhury Shell Programming and Scripting 5 03-13-2008 01:12 AM
error: initializer expression list treated as compound expression arunchaudhary19 High Level Programming 12 11-16-2007 06:44 AM
using regular expression an shell script!! andy2000 Shell Programming and Scripting 3 04-10-2007 11:53 AM
regular expression using a variable barribar Shell Programming and Scripting 0 01-24-2006 07:23 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-01-2006
not4google not4google is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 15
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 (permalink)  
Old 11-01-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
nawk -F',' -v tester=$test1 '
   $0 ~ tester { print $0 }' $inputFile

  #3 (permalink)  
Old 11-01-2006
not4google not4google is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 15
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 (permalink)  
Old 11-01-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
post a snippet from the '$inputFile' and the value of '$tester'
  #5 (permalink)  
Old 11-01-2006
not4google not4google is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 15
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 (permalink)  
Old 11-01-2006
kwachtler kwachtler is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 15
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 (permalink)  
Old 11-01-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:37 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0