How do I count # of char. in a word?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I count # of char. in a word?
# 1  
Old 11-11-2003
How do I count # of char. in a word?

I havent done shell scripting in quite some time. I want to know how to count the number of characters in a word, specifically a parameter.

Example: myscript hello

I want "myscript" to return the number of charcaters in the parameter "hello".

Any ideas?
# 2  
Old 11-11-2003
I suppose there's several ways, but one is awk:

echo "$1" | awk '{print length($0)}'
# 3  
Old 11-11-2003
thanks oombera. I might use this but I'm helping a friend write a script for an Inro. to Unix class and the class doesnt cover the awk command. Could you list any other ways that you know?

and thanks for your help
# 4  
Old 11-11-2003
The length of a parameter is ${#parameter}
# 5  
Old 11-11-2003
please read the rules. no homework questions.

the class might not cover a command but you can bet your $$ that the teacher covered the material needed in class or as a reading assignment.

man wc.
# 6  
Old 11-12-2003
Just as a sidenote, I thought of wc first, but when I used it, it returned one greater than the number of characters in each string it tested, so that "hello" would return "6"... is it counting some end-of-line character?
# 7  
Old 11-12-2003
to tell you the truth i am unsure what the 6th byte is. never noticed it before.

the only thing i can think of is this snipit from the environ(5) man page.

anyone else have any insite into why wc counts more 1 more charicter or byte then is in the file?

Code:
           LC_CTYPE
                 This category  specifies  character  classifica-
                 tion, character conversion, and widths of multi-
                 byte characters. When   LC_CTYPE  is  set  to  a
                 valid value, the calling utility can display and
                 handle text  and  file  names  containing  valid
                 characters for that locale;   Extended Unix Code
                 (EUC) characters where any individual  character
                 can be 1, 2, or 3 bytes wide; and EUC characters
                 of 1, 2, or 3 column  widths.  The  default  "C"
                 locale  corresponds to the 7-bit ASCII character
                 set; only characters from ISO 8859-1 are  valid.
                 The  information  corresponding to this category
                 is  stored  in  a  database   created   by   the
                 localedef()  command.  This environment variable
                 is used by ctype(3C), mblen(3C), and  many  com-
                 mands, such as cat(1), ed(1), ls(1), and vi(1).

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

2. Shell Programming and Scripting

Delete Word between two Char.

HI Guys, I have Input:- A nput A.txt 000100/port_xcu_dev_6/port_0_dev_7 000100/port_xcu_dev_6/port_1_dev_10 000100/port_xcu_dev_2/port_2_dev_8 000100/port_xcu_dev_3/port_3_dev_11 000100/port_xcuv_9/port_4_dev_9 ... (3 Replies)
Discussion started by: pareshkp
3 Replies

3. Shell Programming and Scripting

How count number of char?

hello how can i cont number of char with loop coomand? i dont want to use wc or other special command the script should check all word's char. one by one also a counter can handle the number As noted in other threads started today. This is not the correct forum for homework assignments. ... (2 Replies)
Discussion started by: nimafire
2 Replies

4. Shell Programming and Scripting

Count char, sum and change

Hello, I have some problem in counting char of word, sum and change. I'm not sure shell script can do this. Input data: Sam1 BB BB AA AA BB BB BB Sam2 BB BB AA AA AB AB AB Sam3 BB BB BB AA BB BB BB Sam4 AB AB AB AB AB AB AA Sam5 BB BB AA AA BB BB -- If I count in column 2, B is 9... (3 Replies)
Discussion started by: awil
3 Replies

5. UNIX for Dummies Questions & Answers

Grep char count & pipe to sed command

Hi I am having a 'grep' headache Here is the contents of my file: (PBZ,CP,(((ME,PBZ,BtM),ON),((ME,((PBZ,DG),(CW9,PG11))),CW9,TS2,RT1))) I would like to count out how many times 'PBZ' occurs and then place that number in the line above 3... (8 Replies)
Discussion started by: cavanac2
8 Replies

6. UNIX for Advanced & Expert Users

Word count

Script that lists all words used in one or more files and displays their count (pattern /\W+/ to split the lines of the input file into words can b used).. It should display list in format word:count...gets Filename as an cmd line argument! eg: $perl test doc (where doc is d file we are going to... (4 Replies)
Discussion started by: aadi_uni
4 Replies

7. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

8. Fedora

word count wc

could someone explain this please. echo aaaa|wc -c 5 echo aaaa|wc -m 5 But I'd expect the count to be 4 Its SunOS 5.8 Thanks in Advance. (5 Replies)
Discussion started by: chaandana
5 Replies

9. UNIX for Dummies Questions & Answers

how to count pariticular char in a location in a file

Hi .. I am having file say at 53rd position it will be as 0's or 1's .. How can i count the total number of 1's and 0's in the files at the 53 rd location. Thanks, Arun (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

10. UNIX for Dummies Questions & Answers

count word

hi, given a file i need to get the first line and secodn line and count each of the line whether the length of first line and second line is the same i don;t know how to get the length of the line...seems like use 'wc' cannot do it... please advice (1 Reply)
Discussion started by: ariuscy
1 Replies
Login or Register to Ask a Question