The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
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
How do I count # of char. in a word? xadamz23 Shell Programming and Scripting 9 11-12-2003 12:19 PM

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 06-25-2008
dcfargo dcfargo is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 45
Count lowercase in a word

I have a file like:


aabbccddDDCCDDCCaabbcc 123
CCaaCCBBCCaaaaaaaCCCaa 234
CCDDCCAACCCCccccccccaa 999

I'd like to print out the 1st word followed by the number of lowercase characters in that word. The words only consist of a few letters.

I was trying something like

awk '{a+=gsub(a,a) b+=gsub(b,b) c+=gusb(c,c) d+=(gsub(
d,d) x=(a+b+c+d) END {print $1, X}' infile >outfile


Thanks so much for your help.
  #2 (permalink)  
Old 06-25-2008
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Code:
echo 'aabbccddDDCCDDCCaabbcc' | awk '{print $0, gsub("[a-z]", ".")}'
  #3 (permalink)  
Old 06-25-2008
dcfargo dcfargo is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 45
I'm using Bash and going with:

awk '{print $1, gsub("[a-z]", ".")}' infile > outfile

appears to be counting all the letters not just the lowercase letters

Thank you
  #4 (permalink)  
Old 06-25-2008
dcfargo dcfargo is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 45
going with [a,b,c,d] worked for some reason so I'll just use that.


Thanks so much.

  #5 (permalink)  
Old 06-25-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,929
The following works in ksh93 and should also work in bash.
Code:
#!/usr/bin/ksh93

while read str num
do
   strU=${str//[^A-Z]}
   print "${str} ${#strU}"
done < file
On your sample file, it produces the following output
Code:
aabbccddDDCCDDCCaabbcc 8
CCaaCCBBCCaaaaaaaCCCaa 11
CCDDCCAACCCCccccccccaa 12
  #6 (permalink)  
Old 06-26-2008
ynir ynir is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 35
another way to do this

Code:
# cat aaa
aabbccddDDCCDDCCaabbcc 123
CCaaCCBBCCaaaaaaaCCCaa 234
CCDDCCAACCCCccccccccaa 999

# for i in `cut -f1 aaa -d ' '`; do echo -n "$i "; echo -n $i | sed 's/[^a-z]//g' | wc -c; done
aabbccddDDCCDDCCaabbcc 14
CCaaCCBBCCaaaaaaaCCCaa 11
CCDDCCAACCCCccccccccaa 10
  #7 (permalink)  
Old 09-01-2008
r0bert r0bert is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 1
similar but more difficult promblem

Hi,

I have a similar problem where i need to count stretches of lower- and upper-case letters within words like

ttgggcTGGCCGCCCCCCAGggc
ttgggcTGGCCGCtgggcttCCCCCAGggc

the output could look like:

ttgggcTGGCCGCCCCCCAGggc lower 5 upper 12 lower 3
ttgggcTGGCCGCtgggcttCCCCCAGggc lower 5 upper 7 lower 6 upper 6 lower 3

any help would be appreciated,

best
robert




Quote:
Originally Posted by dcfargo View Post
I have a file like:


aabbccddDDCCDDCCaabbcc 123
CCaaCCBBCCaaaaaaaCCCaa 234
CCDDCCAACCCCccccccccaa 999

I'd like to print out the 1st word followed by the number of lowercase characters in that word. The words only consist of a few letters.

I was trying something like

awk '{a+=gsub(a,a) b+=gsub(b,b) c+=gusb(c,c) d+=(gsub(
d,d) x=(a+b+c+d) END {print $1, X}' infile >outfile


Thanks so much for your help.
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 06:03 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