Computing.Net > Forums > Solaris > string functions?-shell script

string functions?-shell script

Reply to Message Icon

Original Message
Name: Springna
Date: March 11, 2003 at 18:13:37 Pacific
Subject: string functions?-shell script
OS: Solaris6
CPU/Ram: 800
Comment:

Is there any function in Ksh/Sh to make a string length to fixed length, for example, 5 characters length, and add space from left to the strings if it's length not up to 5?

Thanks,


Report Offensive Message For Removal


Response Number 1
Name: David Perry
Date: March 12, 2003 at 04:49:17 Pacific
Reply: (edit)

The function you are looking for is "typeset"

Option Operation
-Ln Left-justify. Remove leading blanks; if n is given, fill with blanks or truncate on right to length n.

-Rn Right-justify. Remove trailing blanks; if n is given, fill with blanks or truncate on left to length n.

-Zn Same as above, except add leading 0's instead of blanks if needed.

-l Convert letters to lowercase.
-u Convert letters to uppercase.

Here are a few simple examples. Assume that the variable alpha is assigned the letters of the alphabet, in alternating case, surrounded by three blanks on each side:

alpha=" aBcDeFgHiJkLmNoPqRsTuVwXyZ "

Examples of typeset String Formatting Options Statement Value of v
typeset -L v=$alpha "aBcDeFgHiJkLmNoPqRsTuVwXyZ "
typeset -L10 v=$alpha "aBcDeFgHiJ"
typeset -R v=$alpha " aBcDeFgHiJkLmNoPqRsTuVwXyZ"
typeset -R16 v=$alpha "kLmNoPqRsTuVwXyZ"
typeset -l v=$alpha " abcdefghijklmnopqrstuvwxyz"
typeset -uR5 v=$alpha "VWXYZ"
typeset -Z8 v="123.50" "00123.50"


Report Offensive Follow Up For Removal

Response Number 2
Name: Springna
Date: March 12, 2003 at 19:09:52 Pacific
Reply: (edit)

thank you so much, it really helps!


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: string functions?-shell script

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge