LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using printf has a problem (https://www.linuxquestions.org/questions/linux-newbie-8/using-printf-has-a-problem-903214/)

Pinglu 09-15-2011 01:22 PM

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

Chris Dennis 09-15-2011 02:29 PM

It works for me, using the Bash shell.

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

cheers

Chris

Bev 09-15-2011 02:35 PM

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

Bev.

theNbomr 09-15-2011 02:44 PM

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.

David the H. 09-15-2011 02:53 PM

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


All times are GMT -5. The time now is 08:44 AM.