LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-02-2012, 06:25 AM   #1
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Rep: Reputation: Disabled
count


i have written a code to find the number of alphabet 'a' in a file

my code is below
Code:
count=`cat $fname | grep -ic 'a' `
echo $count
where $fname is name of a file

my file contents are:

Aathira
apple
pineapple and
orange

Actually answer should be 7 but its coming as 5
In the string 'Aathira' only one 'a' is counted
How it can be rectified??
 
Old 03-02-2012, 06:33 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
sed 's/[^a]//g' input.txt | tr -d '\n' | wc -c
 
1 members found this post helpful.
Old 03-02-2012, 06:45 AM   #3
coralfang
Member
 
Registered: Nov 2010
Location: Bristol, UK
Distribution: Slackware, FreeBSD
Posts: 836
Blog Entries: 3

Rep: Reputation: 297Reputation: 297Reputation: 297
Firstly the answer should be 4, rather than 5.
The -c option displays how many lines contain an 'a', not the total count of the letter 'a'.

This seems to work:
Code:
count=`fold -w 1 $fname | grep -ic 'a'
echo $count
Explanation:
Print each character of the file's contents line by line, and then pass it to grep.
 
Old 03-02-2012, 07:09 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by catkin View Post
Code:
sed 's/[^a]//g' input.txt | tr -d '\n' | wc -c
Nice, but it seems case insensitive match was a requirement, so i modifier could be used
Code:
count=$(sed 's/[^a]//ig' $fname | tr -d '\n' | wc -c)
Another way
Code:
count=$(perl -ne '$i++ while /a/ig; END{print "$i\n"}' $fname)
 
Old 03-02-2012, 07:35 AM   #5
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
another possibility
Code:
grep -oi 'a' test|wc -w
 
1 members found this post helpful.
  


Reply



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
Warning: [fnn_insert] Column count doesn't match value count at row 1 in bondoq Programming 2 09-27-2011 04:11 PM
DBD::mysql::st execute failed: Column count doesn't match value count at row 1 shifter Programming 2 02-24-2010 07:42 PM
Need a way to count sub-directories and get a total count Mo-regard Linux - Newbie 1 08-14-2009 09:10 AM
help with count genderbender Programming 1 12-23-2008 01:00 PM
Should posts in general count on your post count? Joey.Dale General 16 01-27-2004 01:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:55 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration