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?

WYSIWYG
  • 363
  • 2
  • 3
  • 9
  • 1
    I think this is more to do with the way the terminal interprets backspace - see for example the somewhat related [Behaviour of the backspace on terminal](https://unix.stackexchange.com/a/414164/65304) – steeldriver Jun 26 '18 at 14:48

1 Answers1

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.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936