![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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, |
|
||||
|
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? |
|
||||
|
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, |
|
||||
|
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.. |
|
|||||
|
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.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|