For Programmers: Free Programming Magazines  


Home > Archive > AWK > January 2005 > awk trouble









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 awk trouble
sysfault

2005-01-10, 3:56 pm

http://rafb.net/paste/results/GwY9EX31.html

Hello can anyone tell me what's wrong with the above program, I can't seem
to run it with the given arguments, although I specifically enabled it to
do so, I think. Here is the error I get:

../cutd -d. 4 file
awk: fatal: cannot open file `-d.' for reading (No such file or directory)

No idea where it's coming from, I tried switching the argument parsing
around, no luck. Any help with be greatly appreciated, thanks.


--
A wise man knows he knows nothing.

Jürgen Kahrs

2005-01-10, 3:56 pm

sysfault wrote:

> ./cutd -d. 4 file
> awk: fatal: cannot open file `-d.' for reading (No such file or directory)

After reading ARGV[1], you have to blank it.
Otherwise it is interpreted as a file name.

if ( ARGV[1] ~ /\-d?+/ ) {
dmr=ARGV[1]
+ ARGV[1]=""
FS=substr(dmr,3)
}
sysfault

2005-01-10, 3:56 pm

On Mon, 10 Jan 2005 15:52:23 +0100, Jürgen Kahrs wrote:

> sysfault wrote:
>
> After reading ARGV[1], you have to blank it.
> Otherwise it is interpreted as a file name.
>
> if ( ARGV[1] ~ /\-d?+/ ) {
> dmr=ARGV[1]
> + ARGV[1]=""
> FS=substr(dmr,3)
> }


Ok, I made the modification, now it's saying:
../cutd -d. 4 file

awk: fatal: cannot open file `4' for reading (No such file or directory)
I tried doing ARGV[2]="", but no luck.


--
A wise man knows he knows nothing.

Jürgen Kahrs

2005-01-10, 3:56 pm

sysfault wrote:

> awk: fatal: cannot open file `4' for reading (No such file or directory)
> I tried doing ARGV[2]="", but no luck.


If you want to do really clean option handling
like it is done by proper C applications, you
should it do like Arnold Robbins described it
in the manual:

http://www.gnu.org/software/gawk/ma...Getopt-Function

For example, you will find that your script
currently has no loop index over the arguments.
Ed Morton

2005-01-10, 3:56 pm



sysfault wrote:
> http://rafb.net/paste/results/GwY9EX31.html
>
> Hello can anyone tell me what's wrong with the above program, I can't seem
> to run it with the given arguments, although I specifically enabled it to
> do so, I think. Here is the error I get:
>
> ./cutd -d. 4 file
> awk: fatal: cannot open file `-d.' for reading (No such file or directory)
>
> No idea where it's coming from, I tried switching the argument parsing
> around, no luck. Any help with be greatly appreciated, thanks.


Jurgen's already helped with your specific question (use a loop and
clear the ARGV[]s), but you have some other issues in your script:

1) You allow the user to set the field separator for reading input, but
you don't use that same value for output.
2) You add a space to the end of the output string.

I suggest that instead of writing your loop as:

range=substr(ARGV[2],3,length(ARGV[2]))
....
while (range <= NF) {
printf("%s ", $range)
++range
}

you write it as:

start=substr(ARGV[2],3,length(ARGV[2]))
....
sep=""
for (range=start; range <= NF; range++) {
printf("%s%s", sep, $range)
sep = FS
}

Switching from a "while" to a "for" loop is just for aesthetics -
normally loops over a range of fields are "for"s.

Regards,

Ed.
>
> --
> A wise man knows he knows nothing.
>

sysfault

2005-01-10, 3:56 pm

On Mon, 10 Jan 2005 10:28:42 -0600, Ed Morton wrote:
[color=darkred]
>
>
> sysfault wrote:
>
> Jurgen's already helped with your specific question (use a loop and
> clear the ARGV[]s), but you have some other issues in your script:
>
> 1) You allow the user to set the field separator for reading input, but
> you don't use that same value for output.
> 2) You add a space to the end of the output string.
>
> I suggest that instead of writing your loop as:
>
> range=substr(ARGV[2],3,length(ARGV[2]))
> ....
> while (range <= NF) {
> printf("%s ", $range)
> ++range
> }
>
> you write it as:
>
> start=substr(ARGV[2],3,length(ARGV[2]))
> ....
> sep=""
> for (range=start; range <= NF; range++) {
> printf("%s%s", sep, $range)
> sep = FS
> }
>
> Switching from a "while" to a "for" loop is just for aesthetics -
> normally loops over a range of fields are "for"s.
>
> Regards,
>
> Ed.

How would you use a loop to clear the ARGV variable, how would it look?
I'm learning awk from the man page.

--
A wise man knows he knows nothing.

William James

2005-01-10, 3:56 pm


sysfault wrote:
> http://rafb.net/paste/results/GwY9EX31.html
>
> Hello can anyone tell me what's wrong with the above program, I can't

seem
> to run it with the given arguments, although I specifically enabled

it to
> do so, I think. Here is the error I get:
>
> ./cutd -d. 4 file
> awk: fatal: cannot open file `-d.' for reading (No such file or

directory)
>
> No idea where it's coming from, I tried switching the argument

parsing
> around, no luck. Any help with be greatly appreciated, thanks.


Try this:

#!/bin/awk -f

function usage() {
print "cut [-dDELIM] N <FILE>..."
print " -d: use DELIM instead of whitespace for field separator"
print " from N'th byte, character or field, to end of line"
}

BEGIN {

i = 1
if ( ARGV[i] ~ /^-d/ )
{ FS = substr( ARGV[i], 3)
ARGV[i++] = ""
}

if ( ARGV[i] ~ /^[0-9]/ )
{ range = 0 + ARGV[i]
ARGV[i] = ""
}
else
{ usage()
exit 1
}
}

{ for (i=range; i <= NF; i++)
printf "%s%s", $i, (i<NF ? FS : "\n")
}

Ed Morton

2005-01-10, 3:56 pm



sysfault wrote:

<snip>>
> How would you use a loop to clear the ARGV variable, how would it look?
> I'm learning awk from the man page.
>

Jurgen already gave you the link for that -
http://www.gnu.org/software/gawk/ma...Getopt-Function

Ed.
Ed Morton

2005-01-13, 8:56 pm



sysfault wrote:

<snip>>
> How would you use a loop to clear the ARGV variable, how would it look?
> I'm learning awk from the man page.
>

Jurgen already gave you the link for that -
http://www.gnu.org/software/gawk/ma...Getopt-Function

Ed.
Sponsored Links







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

Copyright 2008 codecomments.com