For Programmers: Free Programming Magazines  


Home > Archive > AWK > December 2004 > Problem with awk using shell variables









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 Problem with awk using shell variables
manjunath.mallesh@gmail.com

2004-12-22, 3:55 am

Hi,

I have written the following awk program, which uses the shell
variables inside the awk program

msg="%s has appeared %s times"

cnt=1
cmd="let cnt=$cnt+1"
optArg[1]="abc"
optArg[2]="def"
num=2
pattern="%s"

rslt=$( echo "$msg" | awk 'BEGIN { x=1;varCnt=1 } { while ( x <= NF ) {
if ( $x == "'$pattern'" ) { print "'${optArg[cnt
]}'";cnt++ } else { print $x } x++ } }' )
echo result is "$rslt"

######Output
result is abc
has
appeared
abc
times

I want the first %s to be replaced by optArg[1], second %s by optArg[2]
& so on.........
Can any one please guide me .......

Thanks in advance
Manjunath

Kenny McCormack

2004-12-22, 8:55 am

In article <1103693496.464609.6770@z14g2000cwz.googlegroups.com>,
manjunath.mallesh@gmail.com <manjunath.mallesh@gmail.com> wrote:
>Hi,
>
>I have written the following awk program, which uses the shell
>variables inside the awk program
>
>msg="%s has appeared %s times"
>
>cnt=1
>cmd="let cnt=$cnt+1"
>optArg[1]="abc"
>optArg[2]="def"
>num=2
>pattern="%s"


([unspecified/unidentifiable] Shell mismash - deleted)

Off-topic in comp.lang.awk. Please try again elsewhere.

Ed Morton

2004-12-22, 3:57 pm



manjunath.mallesh@gmail.com wrote:
> Hi,
>
> I have written the following awk program, which uses the shell
> variables inside the awk program
>
> msg="%s has appeared %s times"
>
> cnt=1
> cmd="let cnt=$cnt+1"
> optArg[1]="abc"
> optArg[2]="def"
> num=2
> pattern="%s"
>
> rslt=$( echo "$msg" | awk 'BEGIN { x=1;varCnt=1 } { while ( x <= NF ) {
> if ( $x == "'$pattern'" ) { print "'${optArg[cnt
> ]}'";cnt++ } else { print $x } x++ } }' )
> echo result is "$rslt"
>
> ######Output
> result is abc
> has
> appeared
> abc
> times
>
> I want the first %s to be replaced by optArg[1], second %s by optArg[2]
> & so on.........
> Can any one please guide me .......


You can't pass a shell array to awk (but you can workaround that, see
below). The right way to pass a shell variable is by using awks -v
option or assigning it on the command line or using ENVIRON. Do NOT jump
between awk and shell as you're doing above as you'll get cryptic errors
for some variable values. See the FAQ at comp.lang.awk and at
comp.unix.shell for details.

Try this:

msg="%s has appeared %s times"

cnt=1
cmd="let cnt=$cnt+1"
optArgs="abc def"
num=2
pattern="%s"

rslt=$( echo "$msg" | awk -vpattern="$pattern" -voptArgs="$optArgs" '
BEGIN { na=split(optArgs,optArg," ") }
{
cnt=1;
for (x=1; x<=NF; x++){
if ( $x == pattern ) {
if (cnt <= na) {
print optArg[cnt++]
} else {
print "ERROR: #patterns (%d) > #optArgs (%d)\n",cnt,na
}
} else {
print $x
}
}
}' )
echo result is "$rslt"

Regards,

Ed.

> Thanks in advance
> Manjunath
>

Kenny McCormack

2004-12-22, 3:57 pm

In article <pemdnVUGmfXl4FTcRVn-vg@comcast.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
(Shell mishmash garbage, mercifully deleted)
....
>You can't pass a shell array to awk (but you can workaround that, see
>below).


Please do not post answers to off-topic posts. It only encourages them.
Thank you for your support.

Ed Morton

2004-12-22, 3:57 pm



Kenny McCormack wrote:
> In article <pemdnVUGmfXl4FTcRVn-vg@comcast.com>,
> Ed Morton <morton@lsupcaemnt.com> wrote:
> (Shell mishmash garbage, mercifully deleted)
> ...
>
>
>
> Please do not post answers to off-topic posts. It only encourages them.
> Thank you for your support.
>


There is nothing off-topic about passing shell variables to awk. That's
about as off-topic as reading files with awk.

Ed.
Kenny McCormack

2004-12-22, 3:57 pm

In article <MfydneH_47jmGFTcRVn-3A@comcast.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
>
>
>Kenny McCormack wrote:
>
>There is nothing off-topic about passing shell variables to awk. That's
>about as off-topic as reading files with awk.


Wrong. Answer the following question (A or B).

Q) Issues having to do with "passing shell variables to awk (or any other
language)" are dependent on the syntax of which language:

A) Shell
B) AWK

Thank you for playing our game. I hope it has enlightened you.

Ed Morton

2004-12-22, 3:57 pm



Kenny McCormack wrote:
> In article <MfydneH_47jmGFTcRVn-3A@comcast.com>,
> Ed Morton <morton@lsupcaemnt.com> wrote:

<snip>
>
>
> Wrong. Answer the following question (A or B).
>
> Q) Issues having to do with "passing shell variables to awk (or any other
> language)" are dependent on the syntax of which language:
>
> A) Shell
> B) AWK
>
> Thank you for playing our game. I hope it has enlightened you.
>


The answer is obviously A + B and in fact it's more dependent on B than
it is A. For example, if someone changes the awk parameter "-v" to be
"-V", then anyone using that to pass arguments to awk needs to change
their script regardless of which shell or batch file they're calling it
from. The fact that this is addresseed in the FAQ with more than just a
"this is OT" answer should be a clue.

Having a slow w Kenny?

Ed.
Rufus V. Smith

2004-12-22, 3:57 pm


"Kenny McCormack" <gazelle@yin.interaccess.com> wrote in message
news:cqc17r$gpo$1@yin.interaccess.com...
> In article <MfydneH_47jmGFTcRVn-3A@comcast.com>,
> Ed Morton <morton@lsupcaemnt.com> wrote:
them.[color=darkred]
>
> Wrong. Answer the following question (A or B).
>
> Q) Issues having to do with "passing shell variables to awk (or any other
> language)" are dependent on the syntax of which language:
>
> A) Shell
> B) AWK
>
> Thank you for playing our game. I hope it has enlightened you.
>


Enlightened only in Zen terms, with that non-answer.

How do I pass variables into AWK from another language?

or

When a marriage goes bad, talk to the wife, it has nothing
to do with the husband.

Rufus


Kenny McCormack

2004-12-22, 3:57 pm

In article <cqc4fr$8b5@netnews.proxy.lucent.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
>
>
>Kenny McCormack wrote:
><snip>
>
>The answer is obviously A + B and in fact it's more dependent on B than
>it is A. For example, if someone changes the awk parameter "-v" to be
>"-V", then anyone using that to pass arguments to awk needs to change
>their script regardless of which shell or batch file they're calling it
>from. The fact that this is addresseed in the FAQ with more than just a
>"this is OT" answer should be a clue.


It has nothing to do with AWK per se. The information that you have
provided is, obviously, OS, shell, and AWK-implementation specific.

I don't see that there is anything here, that I have stated, that can
possibly be in a discussable state. Take it over to comp.unix.shell
- they'll be more than happy to accomodate you.

But just in case you still think the OT trash that you've posted has
anything to do with the AWK language, let me leave you with the following:

Q) Can I put that trash into a file and run it with (any) AWK interpreter?
A) No.

Q) Can I put that trash into a file and run it with at least one commonly
available shell (for at least one commonly available OS)?
A) Yes.

Therefore, it is shell code. (Call me Einstein)

Thomas Toth

2004-12-22, 3:57 pm

Kenny McCormack wrote:
> In article <cqc4fr$8b5@netnews.proxy.lucent.com>,
> Ed Morton <morton@lsupcaemnt.com> wrote:
>
>
>
> It has nothing to do with AWK per se. The information that you have
> provided is, obviously, OS, shell, and AWK-implementation specific.
>
> I don't see that there is anything here, that I have stated, that can
> possibly be in a discussable state. Take it over to comp.unix.shell
> - they'll be more than happy to accomodate you.
>
> But just in case you still think the OT trash that you've posted has
> anything to do with the AWK language, let me leave you with the following:
>
> Q) Can I put that trash into a file and run it with (any) AWK interpreter?
> A) No.
>
> Q) Can I put that trash into a file and run it with at least one commonly
> available shell (for at least one commonly available OS)?
> A) Yes.
>
> Therefore, it is shell code. (Call me Einstein)
>

i am in no way a genious, einstein, but IMHO if it requires knowledge
about awk then this is the right place to post as this knowledge is
nowhere else to be found.

alternatively the OP can go from group to group until the problem is
obsolete cause it will never fit perfectly into any of the
classification bins.

tom
Kenny McCormack

2004-12-22, 3:57 pm

In article <41c9ae2d$0$30037$5402220f@news.sunrise.ch>,
Thomas Toth <user@example.net> wrote:
....
>i am in no way a genious, einstein, but IMHO if it requires knowledge
>about awk then this is the right place to post as this knowledge is
>nowhere else to be found.


It has nothing to do with the AWK language.
You will note that the name of this NG is comp.LANG.awk.

>alternatively the OP can go from group to group until the problem is
>obsolete cause it will never fit perfectly into any of the
>classification bins.


You seem to have mistaken this for comp.awk'n'perl'n'shell.helpme.

Ed Morton

2004-12-22, 3:57 pm



Kenny McCormack wrote:
<snip>
> It has nothing to do with AWK per se. The information that you have
> provided is, obviously, OS, shell, and AWK-implementation specific.


It's neither OS-specific nor shell-specific. It is
awk-implementation-specific with a supporting example in shell.

> I don't see that there is anything here, that I have stated, that can
> possibly be in a discussable state. Take it over to comp.unix.shell
> - they'll be more than happy to accomodate you.


Leave it here. We'll be happy to accomodate the OP too.

> But just in case you still think the OT trash that you've posted has
> anything to do with the AWK language, let me leave you with the following:
>
> Q) Can I put that trash into a file and run it with (any) AWK interpreter?
> A) No.
>
> Q) Can I put that trash into a file and run it with at least one commonly
> available shell (for at least one commonly available OS)?
> A) Yes.
>
> Therefore, it is shell code.


Your argument appears to be that anything involving awk arguments is
off-topic. That's a little like owning a car with a broken steering
column and having your mechanic tell you it's not a car issue because it
doesn't involve the engine.

Lighten up or feel free to keep telling people their OT. Either way the
rest of us will feel free to keep helping them with questions that
relate to the awk language, arguments, or implementations.

(Call me Einstein)
>


Not today....

Ed.
Ed Morton

2004-12-22, 3:57 pm



Kenny McCormack wrote:

> In article <41c9ae2d$0$30037$5402220f@news.sunrise.ch>,
> Thomas Toth <user@example.net> wrote:
> ...
>

<snip>
>
>
> It has nothing to do with the AWK language.
> You will note that the name of this NG is comp.LANG.awk.
>
>
>
>
> You seem to have mistaken this for comp.awk'n'perl'n'shell.helpme.
>


Apparently so do you when it suits you:

http://groups-beta.google.com/group...310a548ae49dc44

Ed.

Kenny McCormack

2004-12-23, 3:55 am

In article <cqcd1m$bi9@netnews.proxy.lucent.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
>
>
>Kenny McCormack wrote:
><snip>
>
>It's neither OS-specific nor shell-specific. It is
>awk-implementation-specific with a supporting example in shell.


Wrong.

It is (obviously) OS-specific (Unix/POSIX is only one of the many platforms
upon which AWK runs).

It is (somewhat less obviously) shell-specific, in that most examples given
here don't work in full generality on, e.g., csh type shells.

Needless to say, this gets really hairy (and off topic) when it turns out
that people's "shell" is COMMAND.COM or other MS-dreck.

>
>Leave it here. We'll be happy to accomodate the OP too.


You will be still be gauchely off-topic.

>
>Your argument appears to be that anything involving awk arguments is
>off-topic. That's a little like owning a car with a broken steering
>column and having your mechanic tell you it's not a car issue because it
>doesn't involve the engine.


Actually, that analogy is not that far off - but you should really say "if
the car was scratched or needed to be re-painted, then of course, you would
not go to a mechanic. You would go to a body shop or a detailer."

>Lighten up or feel free to keep telling people their OT. Either way the
>rest of us will feel free to keep helping them with questions that
>relate to the awk language, arguments, or implementations.


You will be off-topic, and I shall not be shy about telling you so, in the
hope, infinitessimal though it may be, that someday, someway, you will see
the error of your ways.

William James

2004-12-24, 3:55 am


Ed Morton wrote:
> Try this:
>
> msg="%s has appeared %s times"
>
> cnt=1
> cmd="let cnt=$cnt+1"
> optArgs="abc def"
> num=2
> pattern="%s"
>
> rslt=$( echo "$msg" | awk -vpattern="$pattern" -voptArgs="$optArgs" '
> BEGIN { na=split(optArgs,optArg," ") }
> {
> cnt=1;
> for (x=1; x<=NF; x++){
> if ( $x == pattern ) {
> if (cnt <= na) {
> print optArg[cnt++]
> } else {
> print "ERROR: #patterns (%d) > #optArgs

(%d)\n",cnt,na
> }
> } else {
> print $x
> }
> }
> }' )
> echo result is "$rslt"


On my system,
msg="%s has appeared %s times"
causes an error.

If this works only on unix-like systems, it should be discussed at
comp.unix.shell.

Kenny McCormack

2004-12-24, 3:55 am

In article <1103853694.527510.101090@z14g2000cwz.googlegroups.com>,
William James <w_a_x_man@yahoo.com> wrote:
....
>On my system,
>msg="%s has appeared %s times"
>causes an error.
>
>If this works only on unix-like systems, it should be discussed at
>comp.unix.shell.


Indeed.

Well put, sir.

Ed Morton

2004-12-24, 3:55 pm



Kenny McCormack wrote:

> In article <1103853694.527510.101090@z14g2000cwz.googlegroups.com>,
> William James <w_a_x_man@yahoo.com> wrote:
> ...
>

Rubbish. The awk script I posted would work on any system and showing
how it interacts with one of them is perffectly reasonable.
[color=darkred]
>
> Indeed.
>
> Well put, sir.


Like you, William seems to only follow this advice when it suits him. See

http://groups-beta.google.com/group...er=1&q=group:co
mp.lang. awk+author:WIlliam+author:James#doc_50c1
15510db3f582

for one of his recent OS-specific postings. I just love people who
subscribe to Groucho Marx's philosophy - "Those are my principles. If
you don't like them I have others."

Ed.
William James

2004-12-24, 8:55 pm

I am perfectly willing to send posters of DOS questions to
comp.os.msdos.

However, it seems that the unix hierophants are unwilling
to stop discussing unix issues at great length and to take
it to comp.unix.shell.

Let's put it to the test. From now on the rule is
"Don't post solutions that work on only one type of system."
People who come here for Awk information shouldn't
have to separate what they need from a bewildering mass
of details that pertain to an operating system that
is alien and irrelevant to them.

Janis Papanagnou

2004-12-24, 8:55 pm

William James wrote:
> I am perfectly willing to send posters of DOS questions to
> comp.os.msdos.
>
> However, it seems that the unix hierophants are unwilling
> to stop discussing unix issues at great length and to take
> it to comp.unix.shell.


I am astonished how intolerant one can be.

> Let's put it to the test. From now on the rule is
> "Don't post solutions that work on only one type of system."
> People who come here for Awk information shouldn't


People who come here to ask for Awk information will just need
to inspect the thread they created.

People who inspect every thread out of interest may decide what
to read and what to skip by the subject line.

> have to separate what they need from a bewildering mass
> of details that pertain to an operating system that
> is alien and irrelevant to them.


Why don't you just ignore threads that are clearly labelled with
the keywords _awk_ *and* _shell_?

(BTW, it has nothing to do with an "operating system" but rather
with the runtime environment; even on a WinDOS operating system
you may have installed Cygwin or MKS to run awk in a way the OP
has been asking for.)

Janis
Kenny McCormack

2004-12-24, 8:55 pm

In article <cqi83l$vk3$1@online.de>,
Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
....
>I am astonished how intolerant one can be.


I'm going to let you in on a little secret: This is all parody.

....

>(BTW, it has nothing to do with an "operating system" but rather
>with the runtime environment; even on a WinDOS operating system
>you may have installed Cygwin or MKS to run awk in a way the OP
>has been asking for.)


I would imagine you could port COMMAND.COM to Unix as well, if the mood
struck you.

I think it is fair enough to assume that for most people, in most cases, it
is safe to equate the two (I.e., equate using Unix with using some kind of
Unix-y shell and equate using MS-dreck OS with using some MS-drecky shell)

Note: I've never understood the point of Cygwin/MKS/etc. Why bother?
If you want Unix, you know where to find it.

Robert Katz

2004-12-25, 3:55 am

Kenny McCormack wrote:
> In article <1103853694.527510.101090@z14g2000cwz.googlegroups.com>,
> William James <w_a_x_man@yahoo.com> wrote:
> ...
>

Is that what you really mean? Discussing this issue in comp.unix.shell
does not imply, a fortiori, that it should not be discussed here. What
if this great question, is it shell or is it awk, were ruminated over in
both comp.unix.shell and comp.lang.awk? That would fit the criterion
you just proposed, but I don't believe that's what you want. I'm
guessing that your real purpose is to say, "This matter should be
discussed not here, but rather in comp.unix.shell (for one)."
[color=darkred]
>
>
> Indeed.
>
> Well put, sir.
>


Not so well put because, if I post this reply to comp.unix.shell as well
as to this group, I will have satisfied William's standard, but I doubt
that you would find it satisfactory, let alone well put.

Let's see, these comments seem to be on-topic for sci.logic ...

--
Regards,

---Robert
Sponsored Links







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

Copyright 2008 codecomments.com