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
318 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
 
LVL 84

Expert Comment

by:ozo
ID: 18027357
$ record="  a  "
$ typeset recordLength="${#record}"
$ echo $recordLength
5
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

757 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now