LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-15-2011, 01:22 PM   #1
Pinglu
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Rep: Reputation: Disabled
Using printf has a problem


Hi,

I'm trying to use printf to do some formatting. here is my code

printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" "custIdPK" "custId" "prefix" "firstName" "middleName1" "middleName2" "middleName3" "lastName" "suffix" >> output.txt

In my output file, here is the output

custIdPK custId prefix firstName middleName1 middleName2 middleName3 lastName suffix

There is suppose to be a tab between custId and prefix, but in the output it doesn't have the tab,can anyone help me out here?

Thanks
 
Old 09-15-2011, 02:29 PM   #2
Chris Dennis
LQ Newbie
 
Registered: Sep 2007
Posts: 2
Blog Entries: 1

Rep: Reputation: 0
It works for me, using the Bash shell.

What shell, or what version of Linux, are you using?

cheers

Chris
 
Old 09-15-2011, 02:35 PM   #3
Bev
LQ Newbie
 
Registered: Sep 2008
Location: Ontario, Canada
Distribution: Slackware 14.2 64bit
Posts: 9

Rep: Reputation: 17
It works for me also.
If you do a hexdump output.txt, do you see a hex 09 character in the output?

Bev.
 
Old 09-15-2011, 02:44 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Works here, too:
Code:
$ printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" "custIdPK" "custId" "prefix" "firstName" "middleName1" "middleName2" "middleName3" "lastName" "suffix" >> output.txt
$ od -tcx1 output.txt 
0000000   c   u   s   t   I   d   P   K  \t   c   u   s   t   I   d  \t
         63  75  73  74  49  64  50  4b  09  63  75  73  74  49  64  09
0000020   p   r   e   f   i   x  \t   f   i   r   s   t   N   a   m   e
         70  72  65  66  69  78  09  66  69  72  73  74  4e  61  6d  65
0000040  \t   m   i   d   d   l   e   N   a   m   e   1  \t   m   i   d
         09  6d  69  64  64  6c  65  4e  61  6d  65  31  09  6d  69  64
0000060   d   l   e   N   a   m   e   2  \t   m   i   d   d   l   e   N
         64  6c  65  4e  61  6d  65  32  09  6d  69  64  64  6c  65  4e
0000100   a   m   e   3  \t   l   a   s   t   N   a   m   e  \t   s   u
         61  6d  65  33  09  6c  61  73  74  4e  61  6d  65  09  73  75
0000120   f   f   i   x  \n   c   u   s   t   I   d   P   K  \t   c   u
         66  66  69  78  0a  63  75  73  74  49  64  50  4b  09  63  75
0000140   s   t   I   d  \t   p   r   e   f   i   x  \t   f   i   r   s
         73  74  49  64  09  70  72  65  66  69  78  09  66  69  72  73                                       
0000160   t   N   a   m   e  \t   m   i   d   d   l   e   N   a   m   e                                       
         74  4e  61  6d  65  09  6d  69  64  64  6c  65  4e  61  6d  65                                       
0000200   1  \t   m   i   d   d   l   e   N   a   m   e   2  \t   m   i                                       
         31  09  6d  69  64  64  6c  65  4e  61  6d  65  32  09  6d  69                                       
0000220   d   d   l   e   N   a   m   e   3  \t   l   a   s   t   N   a                                       
         64  64  6c  65  4e  61  6d  65  33  09  6c  61  73  74  4e  61                                       
0000240   m   e  \t   s   u   f   f   i   x  \n                                                               
         6d  65  09  73  75  66  66  69  78  0a                                                               
0000252
--- rod.
 
Old 09-15-2011, 02:53 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use [code][/code] tags around your code, to preserve formatting and to improve readability.

I think your problem is simply one of appearance. Tabs are a flexible alignment character, and the actual point on the line where they line up is based on factors like the font you use and the amount of other text that comes between them.

When I run your command and display the text in my shell, which uses a mono font, the two columns do indeed look like there's only about one or two spaces between them. But when I open it in kwrite, which is set to a serif font, they look far apart.

To confirm that there are really tabs there, Send the output through cat -A, which will show you all non-printing characters in the text. Tabs will be indicated with "^I".
Code:
cat -A output.txt

Last edited by David the H.; 09-15-2011 at 02:54 PM.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] A small problem on printf ?? alikamu Programming 6 07-25-2011 10:39 AM
[SOLVED] problem with awk printf culin Linux - Newbie 15 11-28-2010 06:25 AM
printf escaping problem Suinatsa Programming 3 10-25-2006 09:28 AM
printf problem??? Mistro116@yahoo.com Programming 2 11-13-2005 08:45 PM
printf new_user10 Programming 3 09-09-2003 11:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:15 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration