[SOLVED] String length in kornshell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SOLVED] String length in kornshell
# 1  
Old 08-04-2011
[SOLVED] String length in kornshell

In the kornshell you can get the length of a string with

Code:
$ x=abc
$ print ${#x}
3

If the current locale is a multibyte locale, like de_AT.UTF-8, you get the length of the string in bytes, not characters:

Code:
$ x=für
$ print ${#x}
4

Is there an easy way to get the length of a string in characters instead of bytes?

Last edited by radoulov; 08-04-2011 at 06:18 AM.. Reason: Classified as solved.
# 2  
Old 08-04-2011
Which OS and which version of the Korn Shell are you using?


Code:
% print $KSH_VERSION 
@(#)MIRBSD KSH R40 2011/07/16
% print "$x" ${#x}                                                                                                          
für 3

# 3  
Old 08-04-2011
I use ksh88 and serveral ksh93 versions on Solaris and OpenSolaris.

The problem in my case seems to be, that ${#varname} counts bytes per design and not characters. If you are in a single byte locale, like iso-8859, then everything is fine, because every character uses exactly one byte.

Get me right, I don't think, this is a bug. I am just asking, if someone is aware of a construct in kornshell 88 or at least 93, that counts characters in every scenario.
# 4  
Old 08-04-2011
I believe it's impossible in ksh88 and in ksh93 it may depend on the version:
Code:
$ ksh --version
  version         sh (AT&T Research) 93t+ 2010-06-21
$ a='良い一日'
$ echo ${#a}
4

For different systems I think it's better to use something else. Perl 5.8 or later is a good choice.
# 5  
Old 08-04-2011
Quote:
Originally Posted by hergp
I use ksh88 and serveral ksh93 versions on Solaris and OpenSolaris.

The problem in my case seems to be, that ${#varname} counts bytes per design and not characters. If you are in a single byte locale, like iso-8859, then everything is fine, because every character uses exactly one byte.

Get me right, I don't think, this is a bug. I am just asking, if someone is aware of a construct in kornshell 88 or at least 93, that counts characters in every scenario.
I understand, I just cannot reproduce it:

Code:
$ locale
LANG=
LC_CTYPE="it_IT.UTF-8@euro"
LC_NUMERIC="it_IT.UTF-8@euro"
LC_TIME="it_IT.UTF-8@euro"
LC_COLLATE="it_IT.UTF-8@euro"
LC_MONETARY="it_IT.UTF-8@euro"
LC_MESSAGES="it_IT.UTF-8@euro"
LC_ALL=it_IT.UTF-8@euro
$ uname -sr
SunOS 5.8
$ Version M-11/16/88i
$ print "$x" ${#x}
für 3

P.S. Hm ...


Code:
$ locale
LANG=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_ALL=en_US.UTF-8
$ print ${.sh.version} "$x" ${#x}
Version M-12/28/93d für 2

# 6  
Old 08-04-2011
Code:
$ locale
LANG=
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_ALL=de_AT.UTF-8
$ uname -sr
SunOS 5.11
$ echo $KSH_VERSION
Version JMP 93t+ 2009-10-12
$ print "$x" ${#x}
für 4

Terminal program (putty) configured to use UTF-8 too, of course.
# 7  
Old 08-04-2011
Code:
$ a='良い一日' 
$ echo ${#a}  
4
$ LANG=
$ echo ${#a}
12

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String Length

Hi All, One of my source file is having Date column and the format of the column is YYYY-MM-DD. As per my business logic I have to check if the date format either YYY-MM-DD or YYYY-M-DD. If any records are in this format then I have print all the records and send those invalid records through... (4 Replies)
Discussion started by: suresh_target
4 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Length of each string

Hi I have a file which has sequences which look like this >String1 aqwertrtrytytyuuijhgddfghhhghhgjhjhhsswekrkmygppdslxmvbnhkwqalldrtjbllnlnlnnnvc >String2 qwwerrtyuiopasdfghjmnbvfklzxerbvcwghjjkoowwqerrtggbddqsdfgaqwcxzakjtyugfsdefrtgyhujiknbbbbcdcdcxsxsx zxzxcvcfcdcg >String3... (5 Replies)
Discussion started by: sa@@
5 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Listing files starting with p or f and with the exact length of 3 characters

Hello, I need some help. How can I list files starting with p or f and with the exact length of 3 characters? (2 Replies)
Discussion started by: airbebe
2 Replies

4. Shell Programming and Scripting

String with different length

let image that we have string: QQQQQQQ:ABCDE:FFFFFF:GGGGG in second field can be 0 or 5 characters if A exist i need set variable ex: VAR=yes if B exist i need set variable ex: VAR1=yes if C exist i need set variable ex: VAR2=yes etc ... if second field is empty no variable to set if... (4 Replies)
Discussion started by: vikus
4 Replies

5. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

6. Programming

regarding string length

Helo, I have character array of sixe 128 char filename now I have one problem that when I enter filename as nothing I got value as " " ",`\0` " . when I find this string length ( " ",`\0`) as 1(one). actually I want to make this length as zero. so what should I do (10 Replies)
Discussion started by: amitpansuria
10 Replies

7. UNIX for Dummies Questions & Answers

length of string

Hi lets say i have a variable output="string" how can you find the length of the string contained in this variable? i guess "wc" cannot be used. its only for files. (8 Replies)
Discussion started by: silas.john
8 Replies

8. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

9. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies

10. UNIX for Dummies Questions & Answers

length of the string

Hi all, pls help me in finding the length of the given string, do we need to write a code seperately or is there any command?? pls help. (3 Replies)
Discussion started by: vasikaran
3 Replies
Login or Register to Ask a Question