/dev/stdin and argc[]s 
Author Message
 /dev/stdin and argc[]s

Hi!

I'm trying to write a #!/usr/bin/gawk -f script that reads the file to
process via a < (redirection) operator and the arguments from the
commandline:
{ print } "/dev/stdin"

"So $ scipt.awk < file" does echo the content of the file, but
"$ scipt.awk arg_no_1  < file" complains about not being able to find
the file arg_no_1.

Nonetheless
BEGIN {
for (i = 0; i < ARGC; i++)
             print ARGV[i]

Quote:
}

with the same cmdline only outputs awk and arg_no_1.

This is on a 2.4.19 GNU/Linux system with bash 2.05b.
Any Idea what's wrong?

  -Thomas

--
"Computers are like Old Testament gods; lots of rules and no mercy."
     --Joseph Campbell



Wed, 27 Apr 2005 02:49:49 GMT  
 /dev/stdin and argc[]s

Quote:

> Hi!

> I'm trying to write a #!/usr/bin/gawk -f script that reads the file to
> process via a < (redirection) operator and the arguments from the
> commandline:
> { print } "/dev/stdin"

> "So $ scipt.awk < file" does echo the content of the file, but
> "$ scipt.awk arg_no_1  < file" complains about not being able to find
> the file arg_no_1.

> Nonetheless
> BEGIN {
> for (i = 0; i < ARGC; i++)
>              print ARGV[i]
> }
> with the same cmdline only outputs awk and arg_no_1.

> This is on a 2.4.19 GNU/Linux system with bash 2.05b.
> Any Idea what's wrong?

>   -Thomas

> --
> "Computers are like Old Testament gods; lots of rules and no mercy."
>      --Joseph Campbell

BEGIN {
for (i = 0; i < ARGC; i++)
             print ARGV[i]
ARGC = 1

Quote:
}

--
Dan Mercer

If responding by email, include the phrase 'from usenet'
in the subject line to avoid spam filtering.

Opinions expressed herein are my own and may not represent those of my employer.



Wed, 27 Apr 2005 04:49:30 GMT  
 /dev/stdin and argc[]s
Quote:



> > Hi!

> > I'm trying to write a #!/usr/bin/gawk -f script that reads the file to
> > process via a < (redirection) operator and the arguments from the
> > commandline:
> > { print } "/dev/stdin"

> > "So $ scipt.awk < file" does echo the content of the file, but
> > "$ scipt.awk arg_no_1  < file" complains about not being able to find
> > the file arg_no_1.

> > Nonetheless
> > BEGIN {
> > for (i = 0; i < ARGC; i++)
> >              print ARGV[i]
> > }
> > with the same cmdline only outputs awk and arg_no_1.

> > This is on a 2.4.19 GNU/Linux system with bash 2.05b.
> > Any Idea what's wrong?

> >   -Thomas

> > --
> > "Computers are like Old Testament gods; lots of rules and no mercy."
> >      --Joseph Campbell

> BEGIN {
> for (i = 0; i < ARGC; i++)
>              print ARGV[i]
> ARGC = 1
> }

> --
> Dan Mercer

> If responding by email, include the phrase 'from usenet'
> in the subject line to avoid spam filtering.

> Opinions expressed herein are my own and may not represent those of my employer.
<snip>
> BEGIN {
> for (i = 0; i < ARGC; i++)
>              print ARGV[i]
> ARGC = 1
> }

</snip>
Does this work I wonder?
Would just using ARGC = ARGC - 1, do the trick for setting the first index?
Doesn't seem to..or maybe I misunderstood the advice?

I just dealt with a problem like this...I thought correctly...

What if you have a case of using multiple args to the script, user defined.
say: awk -f -x "thispattern" -i "Thispattern" -x "anotherpattern" filename

Where the options are parsed: ARGV[element] matching "-option" is found,
and ARGV[element + 1] is added to a pattern list. ARGV[ARGC - 1]
is the filename. Pattern comparison, list splitting, etc is handled
by a single user function.  

Use getline < ARGV[ARGC - 1] into array, and
process all inside the BEGIN action.

   while (t++ < p) {
       if (function(list1,array[t])) {
           do_this
           }
       if (function(list2,array[t])) {
           do_that
           }
    }

Which worked well enough for my purposes.
Am I being hopelessly obtuse?



Thu, 28 Apr 2005 06:31:23 GMT  
 /dev/stdin and argc[]s
Hi Thomas,

Try
$ scipt.awk -v parm1=arg_no_1  < file

and inside scipt.awk check value of variable parm1 (need use '-v' to pass
parm(s) ), e,g.

...
BEGIN{
  printf("parm1=%s\n",parm1);

Quote:
}

...


Quote:
> Hi!

> I'm trying to write a #!/usr/bin/gawk -f script that reads the file to
> process via a < (redirection) operator and the arguments from the
> commandline:
> { print } "/dev/stdin"

> "So $ scipt.awk < file" does echo the content of the file, but
> "$ scipt.awk arg_no_1  < file" complains about not being able to find
> the file arg_no_1.

> Nonetheless
> BEGIN {
> for (i = 0; i < ARGC; i++)
>              print ARGV[i]
> }
> with the same cmdline only outputs awk and arg_no_1.

> This is on a 2.4.19 GNU/Linux system with bash 2.05b.
> Any Idea what's wrong?

>   -Thomas

> --
> "Computers are like Old Testament gods; lots of rules and no mercy."
>      --Joseph Campbell



Thu, 28 Apr 2005 09:08:27 GMT  
 /dev/stdin and argc[]s
Thanks for your replies. Actually the first reply from Dan ("ARGC = 1")
did it!

So the result is this (without all the condition testing):

BEGIN { search = ARGV[1]; replace = ARGV[2]; ARGC = 1 }
{ sub(search,replace) } "/dev/stdin"

for e.g.:
$ myreplace.awk 'wget/messed/this/up/bg.png' '../bgimage.png' < in.html
I never found a tool with my distro to do this.

   -Thomas

--
"I think it is time to order my thoughts, my life, my homedir."



Fri, 29 Apr 2005 05:20:24 GMT  
 /dev/stdin and argc[]s
Hi,

Quote:

> $ myreplace.awk 'wget/messed/this/up/bg.png' '../bgimage.png' < in.html
> I never found a tool with my distro to do this.

I think the most usual way to do this is

sed 's,wget/messed/this/up/bg\.png,../bgimage.png,g' in.html

or perhaps:

awk '{gsub("wget/messed/this/up/bg\\.png","../bgimage.png");print}' in.html

I see no need to maintain a separate short script, but it is
a matter of taste.

Stepan Kasal



Fri, 29 Apr 2005 17:39:59 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. using sys.stdin.raw_input and sys.stdin.readline together in a platform independent way

2. /dev/mary.had.a.little.lamb ( more stupid /dev/hft tricks )

3. Crazy ARGC/ARGV behavior

4. argc problem

5. ArgC/ArgV - Command Line Parsing

6. Forth argv and argc

7. Syntax Question: ARGV/ARGC for Eiffel

8. Problem w ruby_options(argc, argv) in embedded Ruby app

9. Accessing argc,argv,environ from a ruby extension?

10. rb_define_method when argc is -1

11. Re^2: argc and *argv in Modula-2 ?

12. argc and *argv in Modula-2 ?

 

 
Powered by phpBB® Forum Software