From: derek.doerr on
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

From: Chris F.A. Johnson on
On 2006-01-15, 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

Quote your variables, or each word is parsed as a separate
argument:

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
>


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
From: Stachu 'Dozzie' K. on
On 15.01.2006, derek.doerr(a)gmail.com <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.

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.

#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?).

--
Feel free to correct my English
Stanislaw Klekot
From: Bill Marcum on
On 14 Jan 2006 16:47:04 -0800, derek.doerr(a)gmail.com
<derek.doerr(a)gmail.com> wrote:
>
> 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?
>
nawk -F'[|]' -v aTsp="$filterTsp" '$0~aTsp,/OSBtelEndSession/' $srcFile

In awk, if you put a variable name inside / /, it matches the name, not
the contents.


--
What is research but a blind date with knowledge?
-- Will Harvey
From: Ed Morton on
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
>

See question 24 in the FAQ
(http://home.comcast.net/~j.p.h/cus-faq-2.html#24).

Ed.
 |  Next  |  Last
Pages: 1 2
Next: export var in case statement