LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   printf \b not working as expected in AWK (https://www.linuxquestions.org/questions/programming-9/printf-%5Cb-not-working-as-expected-in-awk-768599/)

Rockydell 11-12-2009 02:29 AM

printf \b not working as expected in AWK
 
Hi folks,
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.
Code:

BEGIN{
  printf("0000");
  printf("\b1111");
  }

I expected the output to be
Code:

0001111
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:
Code:

00001111
the two blocks of four characters - intact- with some symbol inbetween.

If I view it in hexadecimal it appears as:

Code:

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:

Code:

0001111
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.

R

bigearsbilly 11-12-2009 03:27 AM

it works for me,

Code:

$ awk 'BEGIN{ printf("0000"); printf("\b1111"); }' > 11
$ cat 11
0001111$

are you using a script or quotes?

if you put the script in "" the \b may be seen by the shell before awk

p.s.
most people prefer perl to awk nowadays.

Rockydell 11-12-2009 06:22 AM

I'm using a script called via the "-f" switch on the command line, something like:
Code:

awk -fTestscript.awk
I'm away from home at the moment using a different machine. Its Windows so I can't reproduce your example exactly.

If you open your file 11 with a text editor rather than display it on the screen using cat, does it still work?

Regards
R

bigearsbilly 11-12-2009 09:28 AM

aaaaaaaahhhhh!

Now I see what you're getting at.

well that is completely NORMAL.
it is inserting the ASCII backspace character. it has a value of 8.
the backspace character is a character. It's not a command.

it is only interpreted when you output it, say to a terminal. if you od a file
newlines and tabs are visible but your typical editor puts the appropriate white space in.

do you get my meaning?

for instance, on my machine
printf "\a" causes a beep. now obviously you cannot put a 'beep' in a file. you put the ascii code for what is generally agreed to cause a beep in a file.

Rockydell 11-12-2009 01:01 PM

Yes, I think I was starting to get there. Makes sense.
I have re-arranged my algorithm to work round that. My initial quick-and-dirty code is a wee bit more complicated now, but that's the way it goes. At least I've learned something out of it.

Thanks
R

bigearsbilly 11-13-2009 07:28 AM

yeah, you can even put a \b in a filename!
that can be a bugger.

ls -b sees 'em.

(in fact you can have anything bar a '/' and a '\0' in a file name e.g: beeps, formfeeds ...)


All times are GMT -5. The time now is 10:27 AM.