I have just started to use linux (Ubuntu 9.10) after years of MS Windows / DOS and have come across a little problem in getting printf() to do what I want in AWK. Essentially I want to use the backspace character to delete a previously printed character, which I thought I could do by backing up and overwriting it with something else.
When I run the program and allow the output to write to the terminal it works as I intended but if I direct the output to a file and then open the file with a text editor, the back space is ignored.
The following is a simplified example using an elementary Awk script with nothing more than a couple of print statements.
I expected the output to be
because the 4th zero is overwritten with the first "1" from the next string. It appears like this on the terminal.
If I write to a text file and open it I get:
the two blocks of four characters - intact- with some symbol inbetween.
If I view it in hexadecimal it appears as:
0x00000000: 30 30 30 30 08 31 31 31 31 | 0000.1111
When writing to a file the "\b" has not actually done anything. it's code 0x08 is just blindly dumped into the data stream.
However, if I view the file from the command line using "less" instead of a normal text editor, it appears once again on the screen as intended i.e:
I was going to ask two questions:
* What is going on here?
* How can I back up and overwrite a character when writing to a file?
Now that I have started to write this down I may have twigged what is happening: It is that control characters like "\b" don't do anything to the data as it is being written
? Does it only get interpreted when the next device reads
it? Hence the difference between viewing on the consul and viewing with a text editor is because they act differently when they see "\b"?
I am using AWK to convert data files from one format to another, not to be viewed, but to be read by another program. In which case the program reading the files may not act on "\b" and get the data all wrong?
If I have hit upon the reason here, is there a way I can back-up to overwrite the data?
If not I guess I will just have to change my algorithm in some way to work around it.