Frage

I'm using gawk and I want to know if it's possible to output the line of the script. I checked the gawk's manual and found information on auto-set variables but anything like __LINE__ isn't listed. Thanks

To clarify here I mean the actual line of the awk script not what input I'm reading. So if I do cat foo | script.awk I am looking for the line number in script.awk. Basically I'd like to show the line number that an error occurred on in my script, if possible.

War es hilfreich?

Lösung

No, it does not. Let us know if you'd like suggestions for workarounds, e.g. creating a new awk script from the original awk script:

$ cat tst.sh
tmp="/usr/tmp/tmp.awk"
trap 'rm -f "$tmp"; exit' 0
> "$tmp"
chmod oug+x "$tmp"

awk '{ sub(/__LINE__/,NR); print }' <<! > "$tmp" && "$tmp"
awk 'BEGIN {
    print "this is line", __LINE__
    print "this is line", __LINE__
    print "this is line", __LINE__
}'
!
$ ./tst.sh
this is line 2
this is line 3
this is line 4
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top