FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 06-30-2012, 01:03 PM
"franz.reitinger"
 
Default bash, (g)awk, local variables

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?


ThanX
/franzR
/*
************************************************** **********

* Mail checked by Avira virus scanner (mail.htl-wels.at).
************************************************** **********
*/



--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 06-30-2012, 01:13 PM
Colin Law
 
Default bash, (g)awk, local variables

On 30 June 2012 14:03, franz.reitinger <franz.reitinger@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

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 

Thread Tools




All times are GMT. The time now is 03:20 PM.

VBulletin, Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2007 - 2008, www.linux-archive.org