Solved

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

Posted on 2006-11-27
9
320 Views
Last Modified: 2013-12-26
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}"
0
Comment
Question by:rayskelton
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 18023828
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
0
 
LVL 6

Accepted Solution

by:
JJSmith earned 125 total points
ID: 18023842

Haven't got access to UNIX at the moment - but have you tried changing the Internal Field Seperator.

save_ifs=$IFS

IFS=":"
typeset recordLength="${#record}"
IFS="$save_ifs"

Cheers
JJ
0
 

Author Comment

by:rayskelton
ID: 18027229
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.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 84

Expert Comment

by:ozo
ID: 18027357
$ record="  a  "
$ typeset recordLength="${#record}"
$ echo $recordLength
5
0
 

Author Comment

by:rayskelton
ID: 18027944
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 18028499
IFS=":"
while read record
do

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

done < "$input_file_name"
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18030846
$ 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
0
 

Author Comment

by:rayskelton
ID: 18031277
I'll try this, in a little while. Thanks for the info and will get back with all on the results.
0
 

Author Comment

by:rayskelton
ID: 18032282
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.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
withoutTen challenge 14 119
CSS HELP 6 87
Grunt Copy file to another destination. 1 33
Increment column based of a FK 8 20
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question