user2672807
user2672807

Reputation: 1

awk - Does gawk have anything like the __LINE__ macro?

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.

Upvotes: 1

Views: 290

Answers (1)

Ed Morton
Ed Morton

Reputation: 203985

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

Upvotes: 2

Related Questions