LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-12-2009, 02:29 AM   #1
Rockydell
LQ Newbie
 
Registered: Nov 2009
Location: Newcastle UK
Distribution: Ubuntu 9.10
Posts: 3

Rep: Reputation: 0
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
 
Old 11-12-2009, 03:27 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
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.
 
Old 11-12-2009, 06:22 AM   #3
Rockydell
LQ Newbie
 
Registered: Nov 2009
Location: Newcastle UK
Distribution: Ubuntu 9.10
Posts: 3

Original Poster
Rep: Reputation: 0
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
 
Old 11-12-2009, 09:28 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
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.

Last edited by bigearsbilly; 11-12-2009 at 09:30 AM.
 
Old 11-12-2009, 01:01 PM   #5
Rockydell
LQ Newbie
 
Registered: Nov 2009
Location: Newcastle UK
Distribution: Ubuntu 9.10
Posts: 3

Original Poster
Rep: Reputation: 0
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
 
Old 11-13-2009, 07:28 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
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 ...)
 
  


Reply

Tags
awk, printf


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf is not working on MVL. shiv.singh Linux - General 10 09-17-2009 01:58 AM
printf is not working on MVL. shiv.singh Programming 2 09-02-2009 12:54 AM
tar not working as expected pendal Linux - Newbie 6 07-31-2009 09:52 AM
printf doesn't work in awk MheAd Programming 4 09-24-2008 01:46 PM
need help w/ awk & printf rickenbacherus Programming 3 05-13-2004 07:21 AM


All times are GMT -5. The time now is 03:17 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration