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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

string functions?-shell script

Reply to Message Icon

Name: Springna
Date: March 11, 2003 at 18:13:37 Pacific
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,



Sponsored Link
Ads by Google

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

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"


0

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

thank you so much, it really helps!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Solaris Forum Home


Sponsored links

Ads by Google


Results for: string functions?-shell script

Shell script www.computing.net/answers/solaris/shell-script/3080.html

Need shell script code www.computing.net/answers/solaris/need-shell-script-code/3390.html

Shell script www.computing.net/answers/solaris/shell-script/3242.html