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!

String Format Question 2

Status
Not open for further replies.

percent

IS-IT--Management
Apr 27, 2004
176
US
Here is what I need done - I have a variable $port this variable outputs on a list with different variables($port equals between S0 through S36 I was wondering if there is an easy way to format $port to do the below:

Say $port equals S1 then I would like it to be S01
Say $port equals S11 then I would like it to stay the same
(so what I would like done is add a zero after the S if the number is a single digit number)

I was hoping I would be able to do something like:
$port=formatstringcommand($port)
I beleive the command I need to use is the printf command but I'm not quite sure how to us it.

Any help would be appreciated.


%, 2004
 
Thank you this was helpful

%, 2004
 
A smaller sed solution...
Code:
port=$(echo $port|sed '/S[0-9]$/s/S/S0/')
...or using printf...
Code:
port=$(printf 'S%02d' $((${port#S})))
 
Thank you this was helpful Also

%, 2004
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top