Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If statement

Status
Not open for further replies.

uguess

Technical User
Nov 5, 2004
40
CA
I have file like below

list
1

I want to write a script that will check the file and ad "zero" 0 in front if the number is less then 10. I have written below script but does not work. Please help.


tmp_LIST=`echo list`
len_tmp_LIST=`scat $tmp_LIST | awk '{print length}'`
if ($len_tmp_LIST -eq 1)
then
{
LIST=`echo 0$tmp_LIST`
cat $LIST
}
else
{
LIST=$tmp_LIST
}
fi

 
LIST=`awk '{printf "%02d",$1}' list`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

Put the [tt]if[/tt]'s condition between brackets, not parenthesis. Or use [tt]test[/tt]. Or write it as arithmetic evaluation :
Code:
if [ $len_tmp_LIST -eq 1 ]
then
[gray]# ...[/gray]
fi

[gray]# or[/gray]

if test $len_tmp_LIST -eq 1
then
[gray]# ...[/gray]
fi
[gray]# or[/gray]

if ((len_tmp_LIST==1))
then
[gray]# ...[/gray]
fi

Feherke.
 
Thanks for your reply

The if condition is workign however the applying of "0" if the digit is less then 10 is not working can you please suggest what i should use. Echo or Cat
 
echo is for displaying values.
cat is for displaying file content.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

I still not understand the theory of your script, but if you have only numeric values, the check could be much loosy. Otherwise would be better to check the type too. Some simple alternatives, maybe they will give you another idea :
Code:
while read one_value; do
  printf "%02d\n" $one_value
do < list_file

[gray]# or[/gray]

awk '$1=sprintf("%02d",$1)' list_file

[gray]# or[/gray]

sed '/^[[:digit:]]\([^[:digit:]]\|$\)/s/^/0/' list_file
They behave different for string values.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top