1

I'm using gawk (4.1.3) and it seems I have struck upon a trivial problem. The backspace character doesn't seem to work if it is immediately followed by a newline.

awk 'BEGIN{for(i=1;i<=3;i++) printf("%d,",i); printf("\b\n")}'

gives me 1,2,3, (the last comma isn't gone)

This doesn't get resolved even if I put the \n in a new printf function.

However, if I insert any character between \b and \n (for instance, a space), it works.

If I remove \n it again works.

What is this issue?

1

1 Answer 1

2

\b only moves the cursor, it does not overwrite the text.

To write out a comma separated list in awk, one option is to create a proper record and print it:

BEGIN {
    OFS = ","
    $0 = ""

    for (i = 1; i <= 3; ++i)
      $i = i

    print
}

The output would be 1,2,3.

And for the one-liner crowd:

awk -vOFS=',' 'BEGIN { for (i = 1; i <= 3; ++i) $i = i; print }'

Setting $0 to an empty string is not really necessary here since we only have a BEGIN block and no input data, so I left it out from the one-line variation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .