Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageIn 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.
Post Follow-up to this message
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
>
Post Follow-up to this messageIn 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.
Post Follow-up to this messageKenny 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.
Post Follow-up to this messageIn 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)
Post Follow-up to this messageKenny 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
Post Follow-up to this messageIn 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.
Post Follow-up to this messageKenny 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.
Post Follow-up to this messageKenny 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.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.