Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.misc > #1292

Re: Re (3): HOW2 pass args to awk? and more

From Ed Morton <mortonspam@gmail.com>
Newsgroups comp.os.linux.misc, comp.lang.awk
Subject Re: Re (3): HOW2 pass args to awk? and more
Date 2011-05-31 07:56 -0500
Organization A noiseless patient Spider
Message-ID <is2oh6$ge4$1@dont-email.me> (permalink)
References <pan.2011.05.29.17.46.08@thedarkdesign.free.fr.INVALID> <iruv09$40j$1@dont-email.me> <4de4cb6c$0$17785$426a34cc@news.free.fr> <yga1uzfrqso.fsf@gepard2.akutech-local.de>

Cross-posted to 2 groups.

Show all headers | View raw


On 5/31/2011 7:35 AM, Ralf Fassel wrote:
> * Loki Harfagr<l0k1@thedarkdesign.free.fr.INVALID>
> | awk 'BEGIN {
> | 	arg1 = ARGV[1]
> | 	arg2 = ARGV[2]
> | 	arg3 = ARGV[3]
> | 	print "arg1=" arg1
> | 	print "arg2=" arg2
> | 	print "arg3=" arg3
> | }
> | ' $1 $2 $3
>
> This will only work if you don't do any work in the awk script.

I assume you mean do any work in the body of the script as opposed to the BEGIN 
section, which is not the case in the above script. Tha main problem I see with 
the above is that it doesn't quote the shell positional parameters so if they 
contain spaces you'll get undesirable results.

   The $1
> $2 $3 in that place on the awk command line will be interpreted by awk
> as filenames to read.

Only if you write a script with more than a BEGIN section and you forget to 
delete those arguments before leaving the BEGIN section.

>
> To pass values to awk, use the -v switch.
>
>    $ cat script
>    #!/bin/sh
>    printf "In script: 1,2,3==$1,$2,$3\n"
>    awk -v arg1=$1 -v arg2=$2 'BEGIN { print arg1, arg2 }'
>    $ ./script foo bar
>    In script: 1,2,3==dog,cat,rain
>    dog cat
>
> R'

In general you're right to recommend "-v" but it's not _always_ the right 
option, depending on how you want a couple of conditions handled, so it's good 
to point people to the FAQ:

	http://cfajohnson.com/shell/cus-faq-2.html#Q24

Regards,

      Ed.

Back to comp.os.linux.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: HOW2 pass args to awk? no.top.post@gmail.com - 2011-05-17 06:27 +0000
  Re: HOW2 pass args to awk? Janis Papanagnou <janis_papanagnou@hotmail.com> - 2011-05-17 09:12 +0200
    Re: HOW2 pass args to awk? Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us> - 2011-05-17 00:21 -0700
      Re: HOW2 pass args to awk? Harold Stevens <wookie@jimbo.localdomain> - 2011-05-17 09:17 -0500
      Re: HOW2 pass args to awk? Aragorn <aragorn@chatfactory.invalid> - 2011-05-17 17:36 +0200
    Re (2): HOW2 pass args to awk? no.top.post@gmail.com - 2011-05-29 15:56 +0000
      Re: Re (2): HOW2 pass args to awk? Bill Marcum <bill@lat.localnet> - 2011-05-29 13:05 -0400
        Re (3): HOW2 pass args to awk? no.top.post@gmail.com - 2011-05-30 02:22 +0000
          Re: Re (3): HOW2 pass args to awk? Aragorn <aragorn@chatfactory.invalid> - 2011-05-30 05:09 +0200
  Re: HOW2 pass args to awk? Bill Marcum <bill@lat.localnet> - 2011-05-17 07:55 -0400
    Re (2): HOW2 pass args to awk? no.top.post@gmail.com - 2011-05-29 15:56 +0000
      Re: Re (2): HOW2 pass args to awk? Loki Harfagr <l0k1@thedarkdesign.free.fr.INVALID> - 2011-05-29 17:46 +0000
        Re (3): HOW2 pass args to awk? no.top.post@gmail.com - 2011-05-30 02:22 +0000
          Re: Re (3): HOW2 pass args to awk? Loki Harfagr <l0k1@thedarkdesign.free.fr.INVALID> - 2011-05-30 09:01 +0000
          Re: Re (3): HOW2 pass args to awk? and more Loki Harfagr <l0k1@thedarkdesign.free.fr.INVALID> - 2011-05-31 11:05 +0000
            Re: Re (3): HOW2 pass args to awk? and more Ralf Fassel <ralfixx@gmx.de> - 2011-05-31 14:35 +0200
              Re: Re (3): HOW2 pass args to awk? and more Ed Morton <mortonspam@gmail.com> - 2011-05-31 07:56 -0500
              Re: Re (3): HOW2 pass args to awk? and more Loki Harfagr <l0k1@thedarkdesign.free.fr.INVALID> - 2011-05-31 14:14 +0000
                Re: Re (3): HOW2 pass args to awk? and more Ralf Fassel <ralfixx@gmx.de> - 2011-06-01 10:41 +0200
              Re (4): HOW2 pass args to awk? and more no.top.post@gmail.com - 2011-06-06 10:26 +0000
                Re: Re (4): HOW2 pass args to awk? and more Loki Harfagr <l0k1@thedarkdesign.free.fr.INVALID> - 2011-06-06 14:01 +0000
            Re (4): HOW2 pass args to awk? and more no.top.post@gmail.com - 2011-06-06 04:58 +0000

csiph-web