I have GNU Awk 4.0.1 on ubuntu 14.4
cut.awk
...
FS = Optarg
OFS = FS
if (FS == " ") { # defeat awk semantics
FS = "[ ]"
...
I found this did not work as expected. Adding print
statements to the if statement :
} else if (c == "d") {
if (length(Optarg) > 1) {
printf("Using first character of %s" \
" for delimiter\n", Optarg) > "/dev/stderr"
Optarg = substr(Optarg, 1, 1)
}
print "Optarg is : <"Optarg">"
FS = Optarg
OFS = FS
if (FS == " ") { # defeat awk semantics
FS = "[ ]"
print "using FS : " FS }
~/gawk/examples/prog-> igawk -f cut.awk -- -f1,2 -d ' ' -s
Optarg is : < >
using FS : [ ]
a b c d
apple pear grape
~/gawk/examples/prog-> igawk -f cut_test.awk -- -f1,2 -s -d ' '
Optarg is : < >
using FS : [ ]
a[ ]b[ ]c[ ]d[ ]
a[ ]b[
apple[ ]pear[ ]grape[ ]
apple[ ]pear[
a[b[c[d[e[
a[ b[ c[ d[ e[
so this gives a clue for what is going wrong
adding this to the top of last block
print "In End Block FS is " FS
if (by_fields && suppress && index($0, FS) == 0){
print " Skipping " $0
next }
I get
~/gawk/examples/prog-> igawk -f cut_test.awk -- -f1,2 -d " " -s
Optarg is : < >
using FS : [ ]
a b c d e
In End Block FS is [ ]
Skipping a b c d e
~/gawk/examples/prog-> igawk -f cut_test.awk -- -f1,2 -d " " ( no -s)
Optarg is : < >
using FS : [ ]
a b c d e
In End Block FS is [ ]
a b
So the solution seems to put
if (FS == "[ ]") {FS = " "}
before if ( by_field && suppress &&index($0,FS)
making that change:
~/gawk/examples/prog-> igawk -f cut.awk -- -f1,5 -d " " -s
d e f
d e f g
d
In wc.awk
I think that the print_total should be( no total line with 2):
print_total=(ARGC-i > 1) ( not 2 )
making that change:
~/gawk/examples/prog-> igawk -f wc2.awk -- -lwc uniq.txt cut.txt
i after for loop is : 2
ARGC is : 4 and Optind is :2
5 21 102 uniq.txt
3 9 36 cut.txt
8 30 138 total
~/gawk/examples/prog-> igawk -f wc2.awk -- -l -w -c uniq.txt cut.txt
i after for loop is : 4
ARGC is : 6 and Optind is :4
5 21 102 uniq.txt
3 9 36 cut.txt
8 30 138 total
~/gawk/examples/prog-> igawk -f wc2.awk -- uniq.txt cut.txt
i after for loop is : 1
ARGC is : 3 and Optind is :1
5 21 102 uniq.txt
3 9 36 cut.txt
8 30 138 total
This seems right since Optind is the index in ARGV of the first non-option arg
FEEDBACK
In case someone else has the same problem, it might prove helpful to provide an example of rewind() in action. Everything I try leads to endless recursion.
I agree with Mendel Cooper - this manual is inspirational and an example how a manual should be written. Thank you.
Best Wishes
David L Ward