Link to home
Start Free TrialLog in
Avatar of rayskelton
rayskelton

asked on

Kornshell on Unix-determining the length of a string, how does one compute the length including white spaces

When determining the length of a string, how does one compute the length including white spaces. I would thing the double quotes around the string while capturing the length would work, but does not. The code snippet below returns the length not including the white spaces. Any ideas how to capture the length with white spaces?

Code Snippet:
typeset recordLength="${#record}"
Avatar of Tintin
Tintin

Works just fine for me:

$ ksh
$ a="a b  c"

$ echo "$a"|awk '{print length}'
6

$ echo "${#a}"
6

$ typeset b="${#a}"
$ echo $b
6
ASKER CERTIFIED SOLUTION
Avatar of JJSmith
JJSmith
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rayskelton

ASKER

I haven't tried changing the field seperator and do not see how this would help, but will try. The problem is a fixed record that is padded with a certain number of spaces at the end of the record. The example from tintin doesn't address that issue.
$ record="  a  "
$ typeset recordLength="${#record}"
$ echo $recordLength
5
The record length in the below code snippet is always the length to the last alpha/numeric character and not to the end of the record.  This capture of the length is the same as ozo and works fine, except for the trailing white spaces that I must count.

---------------------------------------------------
cat "$input_file_name" | while read record
do

   typeset totalLength="${#record}"
   print "+++---Total Record Length:$totalLength"

done
IFS=":"
while read record
do

   typeset totalLength="${#record}"
   print "+++---Total Record Length:$totalLength"

done < "$input_file_name"
$ cat length
#!/bin/ksh
while IFS='' read  line
do
  echo "+$line+"
  echo "${#line}"
done <file.txt

$ ./length
+line one with spaces     +
25
+line two with more spaces     +
30
I'll try this, in a little while. Thanks for the info and will get back with all on the results.
Changing the internal file seperator corrected my issue. Since JJSmith was the first to suggest this, I must award points to this person, although I appreciate all input.