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
323 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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