LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 07: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, 07:33 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

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

Rep: Reputation: 96
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, 08:09 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
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, 08:35 AM   #5
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

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


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


All times are GMT -5. The time now is 03:11 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration