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}"
Code Snippet:
typeset recordLength="${#record}"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
$ typeset recordLength="${#record}"
$ echo $recordLength
5
ASKER
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
--------------------------
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"
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
#!/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
ASKER
I'll try this, in a little while. Thanks for the info and will get back with all on the results.
ASKER
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.
$ ksh
$ a="a b c"
$ echo "$a"|awk '{print length}'
6
$ echo "${#a}"
6
$ typeset b="${#a}"
$ echo $b
6