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
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 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