From: Stephane CHAZELAS on 15 Jan 2006 12:47 2006-01-15, 02:05(+00), Stachu 'Dozzie' K.: [...] > You're very close to what you should do. The idea of inserting > $variables inline in AWK is not too good. What would AWK do if your > $variable (which is inserted in /regexp/) contain '/'? Would blow up. That's true. > > #v+ > $ mysecretvar="variable with spaces" > $ printf "%s\nsecond line won't be printed\n" "$mysecretvar" | \ > awk -v "var=$mysecretvar" '$0 ~ var' > #v- > > And remember to use index() function if you don't really need to use > regexp here. It would be faster (no regexp compilation) and more robust > (are you sure $mysecretvar would contain correct regexp? are you sure it > wouldn't match what you don't want?). [...] That's very true, too, but there's a problem with using -v "var=value" '$0 ~ var' especially when value is a regexp as awk expands the ANSI C escape sequences in "value". for instance RE='\\$' (matches a backslash at the end of a record) will be converted to "\$" (matches a dollar) when passed to an awk variable. Instead of awk -v "var=$var" '$0 ~ var' use awk ' BEGIN { var=ARGV[1] delete ARGV[1] } $0 ~ var' "$var" ... -- St?phane
From: Stachu 'Dozzie' K. on 15 Jan 2006 12:55 On 15.01.2006, Stephane CHAZELAS <this.address(a)is.invalid> wrote: > 2006-01-15, 02:05(+00), Stachu 'Dozzie' K.: > [...] >> You're very close to what you should do. The idea of inserting >> $variables inline in AWK is not too good. What would AWK do if your >> $variable (which is inserted in /regexp/) contain '/'? Would blow up. > > That's true. > >> >> #v+ >> $ mysecretvar="variable with spaces" >> $ printf "%s\nsecond line won't be printed\n" "$mysecretvar" | \ >> awk -v "var=$mysecretvar" '$0 ~ var' >> #v- >> >> And remember to use index() function if you don't really need to use >> regexp here. It would be faster (no regexp compilation) and more robust >> (are you sure $mysecretvar would contain correct regexp? are you sure it >> wouldn't match what you don't want?). > [...] > > That's very true, too, but there's a problem with using -v > "var=value" '$0 ~ var' especially when value is a regexp as awk > expands the ANSI C escape sequences in "value". for instance > > RE='\\$' > > (matches a backslash at the end of a record) will be converted > to "\$" (matches a dollar) when passed to an awk variable. Oh, I didn't know that (I haven't use escape sequences in such context before). Thanks for pointing out. > Instead of > > awk -v "var=$var" '$0 ~ var' > > use > > awk ' > BEGIN { > var=ARGV[1] > delete ARGV[1] > } > $0 ~ var' "$var" ... -- Feel free to correct my English Stanislaw Klekot
From: BaptisteM on 16 Jan 2006 05:04
Hi Derek, Try double quote (") your shell var like this : nawk -F'[|]' '/'"$filterTsp"'/,/OSBtelEndSession/' $srcFile It should works.. Regards, BaptisteM derek.doerr(a)gmail.com wrote: > Here's my script: > > #!/bin/bash > > # $1 source file > # $2 timestamp that defined the start of the search > > srcFile=$1 > filterTsp=$2 > > echo Source File: $srcFile > echo TSP Filter: $filterTsp > > nawk -F'[|]' '/'$filterTsp'/,/OSBtelEndSession/' $srcFile > > The file is executed with the following variables: > > ./extract_log.sh log.txt.060113_1912 'Jan 13 18:20:28.49' > > where : > arg1 is the file to search, > arg2 is a date/timestamp to search for (e.g. 'Jan 13 18:20:28.49') and > > > The search using nawk works fine, if i put the data right in a string: > > > nawk -F'[|]' '/Jan 13 18:20:28.49/,/OSBtelEndSession/' > log.txt.060113_1912 > > ...but does not work when i try to call awk from a shell script, > passing in the search criteria via shell scripts. > > This script appears to run, but does not return anything. I've tried > othe forms of nawk (e.g. > > nawk -F'[|]' -v aTsp="$filterTsp" '/aTsp/,/OSBtelEndSession/' $srcFile > but get an error right away. > > Any ideas on how to pass in this time stamp data, via a shell script > and get awk to accept it correctly? > > Regards, > - Derek |