LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-12-2010, 11:20 PM   #1
mora978
LQ Newbie
 
Registered: Aug 2009
Distribution: Debian
Posts: 8

Rep: Reputation: 1
Awk - finding and counting words specific letters within


Hi all!

My problem is, that i need to calculate the number of words containing letter e in a textfile.
Any suggestions?
 
Old 10-12-2010, 11:45 PM   #2
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 100Reputation: 100
Code:
$ cat input 
bc   def       eeee  12 tec
ee aslfjasf                  e2
Code:
$ cat splitter.awk 
#!/usr/bin/awk

{
    count = split($0, a, " ")
    for(i = 1; i <= count; ++i) { print a[i] }
}
Code:
$ awk -f splitter.awk < input | grep -c e
5
 
Old 10-12-2010, 11:56 PM   #3
mora978
LQ Newbie
 
Registered: Aug 2009
Distribution: Debian
Posts: 8

Original Poster
Rep: Reputation: 1
Thumbs up

thanks quanta! my only problem is, that is should do this only with the use of awk :S

Last edited by mora978; 10-13-2010 at 12:02 AM.
 
Old 10-13-2010, 01:05 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,502

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
How about:
Code:
awk '{sum_e+=match($0,"e")}END{print sum_e}' file
 
Old 10-13-2010, 01:10 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
@mora978, try to do your own homework next time. this is a gift for you


Code:
awk '{ 
 # go through each field, check for "e" and increment w
 for(i=1;i<=NF;i++){
    if( $i~/e/ ){ w++  }
 }
}
END{
  print "total: " w
}' file
 
Old 10-13-2010, 01:15 AM   #6
mora978
LQ Newbie
 
Registered: Aug 2009
Distribution: Debian
Posts: 8

Original Poster
Rep: Reputation: 1
Talking

I've just did'nt got the amount of time, thank you ghostdog74, that is the one!
 
Old 10-13-2010, 01:20 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by mora978 View Post
I've just did'nt got the amount of time,
that's not an excuse. If you spend 8 hrs sleeping,use half that amount to study. If you play 4 hrs a day, use half of that amount to study.

Last edited by ghostdog74; 10-13-2010 at 01:22 AM.
 
Old 10-13-2010, 01:41 AM   #8
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 100Reputation: 100
Quote:
Originally Posted by grail View Post
How about:
Code:
awk '{sum_e+=match($0,"e")}END{print sum_e}' file
This count the number of 'e' letter, not the number of word including 'e' character.
 
Old 10-13-2010, 02:48 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,502

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
My bad ... misunderstood question

Here is my alternative then
Code:
awk 'BEGIN{RS="[ \t\n]+"}/e/{sum++}END{print sum}' file

Last edited by grail; 10-13-2010 at 03:01 AM.
 
Old 10-13-2010, 10:45 AM   #10
mora978
LQ Newbie
 
Registered: Aug 2009
Distribution: Debian
Posts: 8

Original Poster
Rep: Reputation: 1
another solution, if someon needs it:
Quote:
BEGIN{RS="[[:space:]+]"}
/e/ {n++}
END{print n}
almost same as above.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get all lines containing 23 specific words with AWK cgcamal Programming 3 11-05-2008 10:51 AM
counting the words in vi editor. dina3e Linux - Software 6 07-16-2008 10:09 PM
counting words in file and sorting them according to their frequency mystical dervish Programming 4 05-10-2007 08:58 AM
Value counting in awk scripts sarajevo Programming 9 10-20-2006 08:24 AM
counting the commented lines using awk [ /* */] itsjvivek Linux - General 8 01-17-2003 08:30 AM


All times are GMT -5. The time now is 09:21 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration