Printf ... some suggestions on using correctly?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printf ... some suggestions on using correctly?
# 1  
Old 04-14-2011
Printf ... some suggestions on using correctly?

Hi all.
Probably a silly question... :-(
This is my script.

Code:
$ cat ./QuoteGrabber.sh
#!/usr/bin/bash
a=$(./DDECmd.exe request -s TOS -t ASK  -i /ESM1)
b=$(./DDECmd.exe request -s TOS -t BID  -i /ESM1)
c=$(./DDECmd.exe request -s TOS -t VOLUME  -i /ESM1)
d=$(./DDECmd.exe request -s TOS -t IMPL_VOL  -i /ESM1)
echo ASK=$a
echo BID=$b
echo VOLUME=$c
echo IMPL_VOL=$d
echo "--------------"
echo $a $b $c $d[/FONT]

This is the output.
Code:
$ ./QuoteGrabber.sh
ASK=1306,75
BID=1306,50
VOLUME=1,023,435
IMPL_VOL=N/A
--------------
 N/A23,435

The last line is not what I expect.:-(
Can someone help me to make a formattated output with 4 values in printf?
man printf does' t help me so much.. :-(
Thanks

Last edited by Scott; 04-14-2011 at 04:06 PM.. Reason: Code tags, please...
# 2  
Old 04-14-2011
I ran it and it worked as expected displaying all fields with spaces in between them.

Are you displaying the entire script?
# 3  
Old 04-14-2011
Thanks for the reply.
This is the whole script.
Code:
$ cat ./QuoteGrabber.sh
#!/usr/bin/bash
symbol='./ESJ1C1290'
a=$(./DDECmd.exe request -s TOS -t ASK  -i $symbol)
b=$(./DDECmd.exe request -s TOS -t BID  -i $symbol)
c=$(./DDECmd.exe request -s TOS -t VOLUME  -i $symbol)
d=$(./DDECmd.exe request -s TOS -t OPEN_INT  -i $symbol)
e=$(./DDECmd.exe request -s TOS -t IMPL_VOL  -i $symbol)
f=$(./DDECmd.exe request -s TOS -t SYMBOL  -i $symbol)
echo ASK=$a
echo BID=$b
echo VOLUME=$c
echo OPEN_INT=$d
echo IMPL_VOL=$e
echo SYMBOL=$symbol
echo "--------------"
echo $a $b $c $d
echo "****************"
printf "%10s %5s %25s %s\n" $a $b $c $d
printf "BID=%10s\t ASK=%10s\t VOLUME=%10s\t IMPL_VOL=%10s\t" $a $b $c $d

this is the whole output (as you can see I have also some problems in using printf )
Code:
$  ./QuoteGrabber.sh
ASK=16,50
BID=16,00
VOLUME=99
OPEN_INT=3,440
IMPL_VOL=N/A
SYMBOL=./ESJ1C1290
--------------
 3,440
****************
 3,440                 99
BID=     IMPL_VOL=    3,440

Moderator's Comments:
Mod Comment Please use code tags.

Last edited by Scott; 04-14-2011 at 04:07 PM..
# 4  
Old 04-14-2011
If the data you're working with uses windows style line-endings, \r\n, the \n is lost during shell parsing (field splitting, and in the terminating case command substitution as well) but the \r would remain. A terminating \r in the values stored in $a, $b, $c, or $d would yield the results you're seeing.

To illustrate:
Code:
$ printf 'Carriage -> \r <- Return\n'
 <- Return>

It looks like 'Carriage -> ' was never printed, but it was (the ">" after 'Return' is from the arrow that follows 'Carriage'). It's just seems that way due to the how the terminal interprets the \r -- the cursor returns to column 1 of the same line and prior output on that line is overwritten with new output until the next newline moves the cursor to the next line. While some text may be clobbered on the screen, if the output were redirected to a file, inspecting it with a tool such as od(1) would show that all the data is indeed there. The \r is only a problem with regard to displaying/printing on a terminal (and possibly some printers).

To confirm, you could pipe the output of the script into od -c and look for carriage returns, \r. You could also try piping the output through tr -d \\r to see if that fixes the problem.

Regards,
Alister

Last edited by alister; 04-14-2011 at 01:50 PM..
# 5  
Old 04-14-2011
Thanks alister!
Your solution solve brillantly the problem. ;-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Suggestions on this script please

i=1 out="" j=`expr 2 * $1` while do out="$out"#"" echo $out ((i=i+1)) done while do print ${out%?} ((i=i+1)) done This script is throwing an error: gurnish:/home/fnb/gurnish/saurabh/scripts> while1 3 expr: 0402-050 Syntax error. # (6 Replies)
Discussion started by: targetshell
6 Replies

2. Solaris

Suggestions for new T5140

I've been busy and fell behind on Sun/Oracle. Forgive me if too basic. I welcome brief, cryptic, or advanced replies. I also welcome noobie information since I may have no clue what's up at the moment. Problem statement: I inherited a computer to set up. I would rather not figure out 8 months... (1 Reply)
Discussion started by: Nevyn
1 Replies

3. UNIX for Advanced & Expert Users

Need suggestions.......

Hello there....i am a final year comp science student.......i am thinking of doing my project on unix platform......which one do u suggest?thanx in advance... (3 Replies)
Discussion started by: theprasad1990
3 Replies

4. Programming

C Beginner Looking For Suggestions

A few weeks ago at the recommendation of people I trust, I bought and started reading Kernighan and Ritchie's (K&R) C Programming Language. For one thing, it's damn thin compared to the O'Reilly Practical C I just finished last month. It covers generally the same stuff but in a much more... (9 Replies)
Discussion started by: deckard
9 Replies

5. Shell Programming and Scripting

Suggestions on input

Hi, I have written a script which calls a process which ends up in a reboot of the system. At the end of the reboot it prompts for login & i need to provide the login details. am not able to figure out hw to do this. Doubt: will echoing login details after calling the process work? for ex:... (1 Reply)
Discussion started by: meera
1 Replies

6. Solaris

Suggestions Req

Hi all, I have worked on HP UNIX and now i have moved to SunSolaris which i never used to work. I am more on programming side like shell and perl scripting. So i want to know from you experts that i need to take care or changes which i code in sun solaris in compared to HP unix. Suggestions... (1 Reply)
Discussion started by: ravi.sadani19
1 Replies

7. UNIX for Advanced & Expert Users

Looking for Suggestions...

We run WebSphere and by default it wants to install everything under /usr. While I can understand the default (everyone has a /usr) I would like to move this over to a dedicated volume group called apps and then setup my lv's and fs's here. Our WebSphere Admin doesn't like this because apparently... (1 Reply)
Discussion started by: scottsl
1 Replies

8. UNIX for Dummies Questions & Answers

Backup suggestions

The current backup procedure we using a tar command in linux. The files are stored in one partition in different folders. The docs stores in day wise folders like ex: /usr/data/xyz/20050129, /usr/data/xyz/20050130 .............etc We using tar & gzip command to take backup everyday. The backup... (3 Replies)
Discussion started by: bache_gowda
3 Replies

9. UNIX for Dummies Questions & Answers

Suggestions wanted ...

All, Have an AMD-K6/2 PC, 20G.Hd along with RH7.2. Wanting to know what I should do in terms of setup (workstation/server) and then what I can do with it? I'd like to learn a DBMS and SQL - can I do this using RedHat? Any suggestions with how I can use/ what I can do with this appreciated. (3 Replies)
Discussion started by: Cameron
3 Replies

10. Filesystems, Disks and Memory

Backup Suggestions.

Hi all, Have two SCO 5.0.5 systems and one Slackware(joy?) system. I've been asked to backup all three systems onto a newly acquired AIT Tape drive that we've installed on one of the SCO boxes. Using the existing cpio backup script on the one SCO works a treat and is really fast (which is... (2 Replies)
Discussion started by: Cameron
2 Replies
Login or Register to Ask a Question