bash, (g)awk, local variables

Colin Law clanlaw at googlemail.com
Sat Jun 30 13:13:54 UTC 2012


On 30 June 2012 14:03, franz.reitinger <franz.reitinger at htl-wels.at> wrote:
> Hi,
> I'm using awk for string manipulations within a (backup)bash script. Among
> others I have to do some kind of substring operations using the built-in
> index-function:
>
> Docu:
> index(in, find)
> This searches the string in for the first occurrence of the string find, and
> returns the position in characters where that occurrence begins in the
> string in. For example:
>
> awk 'BEGIN { print index("peanut", "an") }'
>
> Of course this example works; however I want to replace both literals with
> the content of local bash / environment variables like:
>
> a= $(cat ANY_FILE);
> b="anyString"
>
> awk 'BEGIN { print index($a, $b) }'
>
> Where I'm wrong & how can I use bash variables within built-in functions of
> awk?

I am not an expert but if in a terminal I do
a = "a string"
echo $a
then I see
a string
as expected.  However if I do
echo '$a'
then I see
$a

but echo "$a" shows a string.  So I suggest trying double quotes in
your awk command.

Colin




More information about the ubuntu-users mailing list