The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
word count wc chaandana UNIX for Dummies Questions & Answers 5 05-05-2009 11:47 AM
specified word count bhaviknp Shell Programming and Scripting 5 02-27-2008 11:23 AM
how to count pariticular char in a location in a file arunkumar_mca UNIX for Dummies Questions & Answers 5 07-18-2007 12:40 PM
Word count problem ssmith001 UNIX for Dummies Questions & Answers 1 01-02-2007 01:21 PM
count word ariuscy UNIX for Dummies Questions & Answers 1 10-13-2005 01:36 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-11-2003
xadamz23 xadamz23 is offline
Registered User
  
 

Join Date: Aug 2003
Posts: 31
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 (permalink)  
Old 11-11-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
I suppose there's several ways, but one is awk:

echo "$1" | awk '{print length($0)}'
  #3 (permalink)  
Old 11-11-2003
xadamz23 xadamz23 is offline
Registered User
  
 

Join Date: Aug 2003
Posts: 31
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 (permalink)  
Old 11-11-2003
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
The length of a parameter is ${#parameter}
  #5 (permalink)  
Old 11-11-2003
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
  
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
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 (permalink)  
Old 11-12-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
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 (permalink)  
Old 11-12-2003
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
  
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
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).

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0