find length of a parameter


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find length of a parameter
# 1  
Old 09-07-2008
find length of a parameter

I want to check the length of a parameter passed to the korn shell. I've tested:

xxx=hello
echo $xxx
cnt=`echo $xxx |wc -c`
echo "cnt $cnt"

(xxx will be the parm). $cnt is 6, it should be 5. I keep getting 1 more then I should. Why is that? How do I code to get the length of a parameter?


# 2  
Old 09-08-2008
wc -c counts bytes, not characters. I guess it is counting the \0 as end of the string, so you got one more than you want.
On AIX you can use wc -k or on Debian Linux wc -m to get the number of characters. Do "man wc" which switch is the one you need on your OS.
# 3  
Old 09-08-2008
xxx="Hello"
len=${#xxx}
echo "cnt $len"
# 4  
Old 09-08-2008
Actually wc is counting the newline at the end. Use echo -n or whatever your local echo has for not adding a newline; or better, don't use echo -- the approach offered by manosubsulo is superior.
# 5  
Old 09-17-2008
Thank you for your replies. I could not get them to work correctly. I found another post that worked:
cnt=`expr length $xxx`
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find length of multidimension array ???

Does anyone know how to find length of multi dimension array of following type A Afor simple array I is to do for (i in A)n++ to find length of array but if it is multi dimension how to find the length ? (2 Replies)
Discussion started by: nex_asp
2 Replies

2. Shell Programming and Scripting

Need to find lines where the length is less than 50 characters

Hi, I have a big file say abc.csv. And in that file, I need to find lines whose length is less than 50 characters. How can it be achieved? Thanks in advance. Thanks (4 Replies)
Discussion started by: Gangadhar Reddy
4 Replies

3. Programming

Fastest way to find the length of string in c

Hi all, We use strlen() fun provided by library to find the length of a string. Looking inside of it, it has some different mechanism to find the length of string. Normally, we scan the string byte by byte until the '\0' character. It takes a logn time to count length. The Library strlen()... (2 Replies)
Discussion started by: yogeshrl9072
2 Replies

4. Shell Programming and Scripting

Find the length of a path

Hey, I got a question: how do I find the length of a path ? this is the context: I used this funtion find: DIR="/home/name/directory" find DIR -name "*.extension" The output is then: /home/name/directory/filename.extension What I'd like to have is only the last part:... (3 Replies)
Discussion started by: Miki1579
3 Replies

5. Shell Programming and Scripting

Find the length of each line in the file

Hi, I want to find the length of each line(including all the characters, spaces etc.) in a file and check if all the lines are of same length using a ksh script. Please help. Thanks in advance. (7 Replies)
Discussion started by: Suryaaravindh
7 Replies

6. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

7. Shell Programming and Scripting

help need to find the length of a variable

Hi frnds, i have a file which contains multiple lines. each line size may come around 64MB. and i want to read line by line and nee to find the length of each line, at present am doing it in the following way, while read LINE do len=`expr length "$LINE"` # and using the variable... (7 Replies)
Discussion started by: smr_rashmy
7 Replies

8. Shell Programming and Scripting

Find the length of the longest line

Dear All, To find the length of the longest line from a file i have used wc -L which is giving the proper output... But the problem is AIX os does not support wc -L command. so is there any other way 2 to find out the length of the longest line using awk or sed ? Regards, Pankaj (1 Reply)
Discussion started by: panknil
1 Replies

9. UNIX for Dummies Questions & Answers

Need find a file based length

Can some please help me? Want to find files over 35 characters in length? I am running HPUX. Would it be possible with find? Thanks in advance (8 Replies)
Discussion started by: J_ang
8 Replies
Login or Register to Ask a Question