Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Problem with awk using shell variables
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


Report this thread to moderator Post Follow-up to this message
Old Post
manjunath.mallesh@gmail.com
12-28-04 08:56 PM


Re: Problem with awk using shell variables
Xref: newsfeed-west.nntpserver.com comp.lang.awk:24105



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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
12-28-04 08:56 PM


Re: Problem with awk using shell variables
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.


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
12-28-04 08:56 PM


Re: Problem with awk using shell variables
In article <1104247198. c9ce90fdd1d6081b4c1d43dd9db8275f@teranew
s>,
Rufus V. Smith <nospam@nospam.com> wrote:

% "Patrick TJ McPhee" <ptjm@interlog.com> wrote in message
% news:33c7orF3ucabeU1@uni-berlin.de...
% > In article <cqia72$neq$1@yin.interaccess.com>,
% > Kenny McCormack <gazelle@interaccess.com> wrote:
% >
% > % I would imagine you could port COMMAND.COM to Unix as well, if the moo
d
% > % struck you.
% >
% > It's been done.

% Usually a statement like that is followed by some sort of
% qualifying reference, be it URL, magazine article, book,
% program name.

Actually, a statement like that is practically never followed
by any sort of qualifying reference at all. The program that I
was referring to was called command. I hope this is helpful.



--

Patrick TJ McPhee
North York  Canada
ptjm@interlog.com

Report this thread to moderator Post Follow-up to this message
Old Post
Patrick TJ McPhee
12-29-04 08:55 AM


Re: Problem with awk using shell variables
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

Report this thread to moderator Post Follow-up to this message
Old Post
Janis Papanagnou
12-30-04 08:55 AM


Re: Problem with awk using shell variables
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.


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
12-30-04 08:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 08:35 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.