For Programmers: Free Programming Magazines  


Home > Archive > AWK > June 2004 > Single quotes in shell script for awk command









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Single quotes in shell script for awk command
mehedi

2004-06-09, 8:55 pm

the following commands are within a shell script

#Read first line from file and assign value to variable var
var=`awk 'BEGIN {getline tmp; print tmp }' < TOSEARCH2`
echo $var
#build command
var1="awk '/"$var"/ , /END/' INPRO.fdl"
#execute command
var2="`$var1`"
--
While exectuing this line the commands errors see below.How do i get
this to work ?

var2="`$var1`"
+ + awk '/EOEPROCA10/ , /END/' INPRO.fdl
Syntax Error The source line is 1.
The error context is[color=darkred]
awk: 0602-500 Quitting The source line is 1.
var2=
echo $var2
+ echo
Patrick TJ McPhee

2004-06-10, 3:55 am

In article <ac0fd59d.0406091135.644eacc6@posting.google.com>,
mehedi <mehedi.hashir@ibx.com> wrote:

% the following commands are within a shell script

[...]

% var2="`$var1`"
% + + awk '/EOEPROCA10/ , /END/' INPRO.fdl
% Syntax Error The source line is 1.


To make this work, you need to use eval:

var2="`eval $var1`"

However, you don't have to do it this way. As a young person of my
aquaintance is fond of saying, `think of the children'.


% #Read first line from file and assign value to variable var
% var=`awk 'BEGIN {getline tmp; print tmp }' < TOSEARCH2`

How about
var2=$(awk 'BEGIN { getline tosearch < "TOSEARCH2" }
$0 ~ tosearch, /END/' INPRO.fdl)
?
--

Patrick TJ McPhee
East York Canada
ptjm@interlog.com
Ed Morton

2004-06-10, 3:55 am


mehedi wrote:
> the following commands are within a shell script
>
> #Read first line from file and assign value to variable var
> var=`awk 'BEGIN {getline tmp; print tmp }' < TOSEARCH2`


If you really want to use awk, the above would be:

var=`awk '{print;exit}' TOSEARCH2`

but you could just use read:

IFS= read var < TOSEARCH2

> echo $var


Make that:

echo "$var"

> #build command
> var1="awk '/"$var"/ , /END/' INPRO.fdl"
> #execute command
> var2="`$var1`"
> --
> While exectuing this line the commands errors see below.How do i get
> this to work ?


Change the above to:

var1="awk -vvar=\"$var\" '\$0 ~ var , /END/' INPRO.fdl"
var2=`eval "$var1"`

If your awk doesn't support "-v", then I strongly recommend you get a
version of awk that does (e.g. gawk), but otheriwse changing the first
line to this:

var1="awk '\$0 ~ var , /END/' var=\"$var\" INPRO.fdl"

should work. Note that in both cases this evaluates "$var" when var1 is
assigned, not when it's executed. If you want the evaluation done at
execution time, change the line to escape the $ at the start of $var:

var1="awk ... var=\"\$var\" ..."

Regards,

Ed.

Ed Morton

2004-06-10, 3:55 am



Patrick TJ McPhee wrote:

<snip>
> How about
> var2=$(awk 'BEGIN { getline tosearch < "TOSEARCH2" }
> $0 ~ tosearch, /END/' INPRO.fdl)
> ?


Or even:

var2=$(awk 'NR == FNR{ tosearch = $0; next }
$0 ~ tosearch, /END/' TOSEARCH INPRO.fdl)

There's just something about "getline" that makes me queasy. Having said
that, I can't say I'm real fond of "next" either but I find myself
constantly using it 8-).

Regards,

Ed.

mehedi

2004-06-10, 8:57 pm

This brings me to the next question.

This is for the 1st record in the file
var2=$(awk 'NR == FNR{ tosearch = $0; next }
$0 ~ tosearch, /END/' TOSEARCH INPRO.fdl)
var2=$(awk 'BEGIN { getline tosearch < "TOSEARCH2" }
$0 ~ tosearch, /END/' INPRO.fdl)
How do i read all the records in the file(TOSEARCH2) and use them to
search through a file(INPRO.fdl here)

Thanks Ed & Morton
Regards

Mehedi









Ed Morton <morton@lsupcaemnt.com> wrote in message news:<X7Cdnfe1CutPQ1rdRVn-hQ@comcast.com>...
> Patrick TJ McPhee wrote:
>
> <snip>
>
> Or even:
>
> var2=$(awk 'NR == FNR{ tosearch = $0; next }
> $0 ~ tosearch, /END/' TOSEARCH INPRO.fdl)
>
> There's just something about "getline" that makes me queasy. Having said
> that, I can't say I'm real fond of "next" either but I find myself
> constantly using it 8-).
>
> Regards,
>
> Ed.

Ed Morton

2004-06-10, 8:57 pm



mehedi wrote:
> This brings me to the next question.
>
> This is for the 1st record in the file
> var2=$(awk 'NR == FNR{ tosearch = $0; next }
> $0 ~ tosearch, /END/' TOSEARCH INPRO.fdl)
> var2=$(awk 'BEGIN { getline tosearch < "TOSEARCH2" }
> $0 ~ tosearch, /END/' INPRO.fdl)
> How do i read all the records in the file(TOSEARCH2) and use them to
> search through a file(INPRO.fdl here)


Please don't top-post.

The easiest way is probably using shell and awk like you were doing, e.g.:

while IFS= read tosearch
do
awk -vtosearch="$tosearch" '$0 ~ tosearch, /END/' INPRO.fdl
done < TOSEARCH

By the way, FYI GNU grep supports a format like:

grep -f TOSEARCH INPRO.fdl

which will find every string in TOSEARCH, but it won't get the block to
/END/.

> Thanks Ed & Morton


You're welcome from both of me ;-). I assume you meant Ed & Patrick.

Ed.[color=darkred]
> Regards
>
> Mehedi
>
>
>
>
>
>
>
>
>
> Ed Morton <morton@lsupcaemnt.com> wrote in message news:<X7Cdnfe1CutPQ1rdRVn-hQ@comcast.com>...
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com