Need help in printf in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in printf in shell script
# 15  
Old 04-18-2013
the length will be from runtime ... so i am not sure abt the how many zero to give
# 16  
Old 04-18-2013
Quote:
Originally Posted by greenworld123
Mine is ksh ... it is not working
Code:
length=15
value=""
printf "%0*d\n" $length "$value"
*d
*d

If this isn't working, you must be using an 1988 version of ksh. If you are on a Solaris/SunOS system, you could try /usr/xpg4/bin/sh or /usr/xpg6/bin/sh to get a 1993 version of ksh. You could also look for a ksh93 utility to use instead of plain ksh. Even if the ksh built-in printf doesn't recognize format specifiers like %0*d, there might be a /bin/printf or /usr/bin/printf that does understand what to do with it.
# 17  
Old 04-18-2013
There is no options in
Code:
awk , sed ...

# 18  
Old 04-18-2013
Quote:
the length will be from runtime
You're right. Maybe this then:
Code:
if [ -z "value" ]; then
  value=0
fi
typeset -RZ${length} value
echo $value

There is also a ${value:=0} syntax you could use that sets "value" to 0 if value is not set or is null.

---------- Post updated at 02:46 AM ---------- Previous update was at 02:41 AM ----------

So in other words, you might be able to just say:
Code:
typeset -RZ${length} value
echo ${value:=0}

# 19  
Old 04-18-2013
Quote:
Originally Posted by hanson44
Code:
$ cat temp.sh
length=15
value="1234567890"
printf "%0*d\n" $length "$value"
 
$ ./temp.sh
000001234567890


that sounds grt to me .. it worked...

I have another problem now

i want to find a string/number variable value length...
Code:
ex:
value=value=`echo "$i_line1" | cut -d"|" -f4` ( this will be from run time .. i mean reading this value from another file which is pipe delimiter)
value_len=`echo  "$value\c" | wc -m`

if value is empty.. it is stil giving value_len as 15...
it should be empty ...not sure what is the problem

---------- Post updated at 05:51 AM ---------- Previous update was at 04:52 AM ----------

i searched and used atlast
Code:
 
value_len=`echo  ${#value}`

# 20  
Old 04-18-2013
Sounds like it is working now. In case not:

Why do you say "value=value=`echo" instead of just "value=`echo" expression?

It would help to have some diagnostic lines like "echo $i_line1" and "echo $value" all by themselves to help track down what is going on.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem running plsql using printf command on bash shell

I am running plsql using printf on a shell, but i am getting some strange error, can someone point what exactly am i missing, $ echo $SHELL /bin/bash $ printf " > SET serveroutput ON trimspool on feed off echo off > declare > p_val number; > d_val varchar2(10); > begin > SELECT... (1 Reply)
Discussion started by: kamauv234
1 Replies

2. UNIX for Beginners Questions & Answers

How to use printf to output a shell variable path?

So I created two shell variables: COLUMN1_HEADING, COLUMN2_HEADING. They have values: COLUMN1_HEADING="John" COLUMN2_HEADING="123456789" How would I use printf to get it to print an output like this: $COLUMN1_HEADING\t$COLUMN2_HEADING\nJohn\t123456789\n Thanks! (3 Replies)
Discussion started by: steezuschrist96
3 Replies

3. Shell Programming and Scripting

Shell Printf command , a little more dynamic..

A big hello to everyone tagged to this site of knowledge . This is the first post of mine and I am looking forward to an enjoyable stint in this forum where I get to know a lot of new ideas and share whatever knowledge (its not much though :) ) I have acquired throughout my career so far with... (4 Replies)
Discussion started by: kumarjt
4 Replies

4. Shell Programming and Scripting

printf (awk,perl,shell) float rounding issue

Hi guys, could someone throw some light on the following behaviour of printf (I'll start with info about the system and the tool/shell/interpreter versions)?: $ uname -a Linux linux-86if.site 3.1.0-1.2-desktop #1 SMP PREEMPT Thu Nov 3 14:45:45 UTC 2011 (187dde0) x86_64 x86_64 x86_64... (9 Replies)
Discussion started by: elixir_sinari
9 Replies

5. Shell Programming and Scripting

Help with printf in shell scripting

Hi all I am using printf in my shell script to format my output.I am using the following code. printf "|\tIP Address\t|\tModel Number\t|\tDOM/HW\t|\tSoftware Version\t|\n" printf "|\t%s\t|\t%s\t\t|\t%s\t|\t%s-%s\t\n" $encoder $modelno $type $sv1 $sv2 printf "|\tFPGA... (2 Replies)
Discussion started by: ramman
2 Replies

6. Shell Programming and Scripting

Problem with printf in UNIX KSH shell

Hi ALL, I am using SunOS 5.9 and KSH(bin/ksh) The problem am facing is error message diaplyed on screen printf: 12099415.79 not completely converted printf: + expected numeric value printf: 11898578.29 not completely converted When i try printing with The output is... (6 Replies)
Discussion started by: selvankj
6 Replies

7. Shell Programming and Scripting

problem with printf in shell script

i have written small script as follows: name="hi hello" printf "%-20s" $name This gives me strange output. -20s format is applied on both word of string. i.e it displays both word hi and hello in space of 20 length. I want to display entire string "hi hello" in length of 20 space. plz... (2 Replies)
Discussion started by: admc123
2 Replies

8. Shell Programming and Scripting

printf in bash shell not printing negative values

hi i am using printf in a script and it is not printing negative values..i have to use printf to get rid of the newline..here is my code: fin=`echo $a - $b | bc` printf "${fin}," >> test these statements are in a loop. here is what i get when i try to subtract 4 from 8: ./scr1: line... (2 Replies)
Discussion started by: npatwardhan
2 Replies
Login or Register to Ask a Question