- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- number of occurances of a character
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2004 03:45 AM
10-14-2004 03:45 AM
number of occurances of a character
of a particular character in an unix file?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2004 03:52 AM
10-14-2004 03:52 AM
Re: number of occurances of a character
That will give you a count
count=$(cat filename | wc -m)
echo $count
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2004 03:52 AM
10-14-2004 03:52 AM
Re: number of occurances of a character
:%s/a/A/g
It will tell you "437 substitutions". Then don't save the file.
I had hoped that "ed" would report a similar output so you could do: -
ed file <
q
EOF
and read the output, but it seems not to. This should get you started though.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2004 03:55 AM
10-14-2004 03:55 AM
Re: number of occurances of a character
# freq file
And in perl (not tested)
# perl -ne'$c{$_}++ for split//;END{for(sort keys%c{printf"%3d %2s %6d\n",ord$_,$_ lt" "?"^".($_|"@"):$_,$c{$_}}}' file
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2004 03:58 AM
10-14-2004 03:58 AM
Re: number of occurances of a character
a5:/u/usr/merijn 109 > freq .Xdefaults | grep -w a
|soh 0|! 91|A 8|a 335|
a5:/u/usr/merijn 110 > perl -nle'END{print$a}$a+=(tr/a/a/)' .Xdefaults
335
a5:/u/usr/merijn 111 >
Enjoy, Have Fun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2004 04:02 AM
10-14-2004 04:02 AM
Re: number of occurances of a character
echo "abcadefgabf\nkhaeeaf" | awk -Fa '{s=s+NF-1};END{print s}'
This will count number of "a" in file.
Here is how it can be done with "perl".
echo "abcadefgabf\nkhaeeaf" | perl -ne '$n=s/a//g;$s+=$n;END{print $s,"\n";}'
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2004 04:06 AM
10-14-2004 04:06 AM
Re: number of occurances of a character
syntax error in file /tmp/perl-ea02209 at line 1, next 2 tokens "END {"
syntax error in file /tmp/perl-ea02209 at line 2, next token "}"
Execution of /tmp/perl-ea02209 aborted due to compilation errors.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2004 04:21 AM
10-14-2004 04:21 AM
Re: number of occurances of a character
a5:/u/usr/merijn 112 > /usr/contrib/bin/perl -nle'END{print$a}$a+=(tr/a/a/)' .Xdefaults
syntax error in file /tmp/perl-ea03721 at line 1, next token "."
Execution of /tmp/perl-ea03721 aborted due to compilation errors.
Exit 2
a5:/u/usr/merijn 113 >
That's from perl4
perl4 should be happy with this:
a5:/u/usr/merijn 124 > /usr/contrib/bin/perl -le 'while(<>){$a+=y/a/a/}print$a' .Xdefaults
335
a5:/u/usr/merijn 125 >
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2004 04:26 AM
10-14-2004 04:26 AM
Re: number of occurances of a character
I do not have freq command. I am on 11i.
where is that command.
Anil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2004 05:18 AM
10-14-2004 05:18 AM
Re: number of occurances of a character
-> Count a on each line - number of lines
Regards,
Fred
"Reality is just a point of view." (P. K. D.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2004 05:50 AM
10-14-2004 05:50 AM
Re: number of occurances of a character
Old but VERY useful (when you need it)
Source attached
I'm so used to having it that I forgot It was from source.
On some systems (like Linux) you'll need an additional
#include
Enjoy, Have FUNE H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2004 07:46 AM
10-14-2004 07:46 AM
Re: number of occurances of a character
you can do it with a little workaround - at least I think so. Example of an input file:
# cat infile
abc
defg
hij
kl
amn
opq
raa
sta
Count all chars of infile:
# wc -c infile
32 infile
Now fake a deletion of the char in question, e.g. "a", and count the remaining:
# tr -d a
Just a suggestion...
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2004 07:48 AM
10-14-2004 07:48 AM
Re: number of occurances of a character
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2005 05:05 AM
10-05-2005 05:05 AM
Re: number of occurances of a character
I modified the awk to find the number of occurrences of "/". I find that the perl does not seem to be able to handle this as written (is further modification required?)Here are the versions of your code with "/" substituted for "a" (see post from Rodney Hills above for original code):
Works:
#echo "/m03/oracle/oradata" | awk -F/ '{s=s+NF-1};END{print s}'
3
Does Not Work:
#echo "abcadefgabf\nkhaeeaf" | perl -ne '$n=s/\//g;$s+=$n;END{print $s,"\n";}'
Substitution replacement not terminated at -e line 1.
Also Does not Work (gives "incorrect" result):
#echo "abcadefgabf\nkhaeeaf" | perl -ne '$n=s/\\//g;$s+=$n;END{print $s,"\n";}'
0
Is additional code required in the perl example to handle special characters like "$", ".", "/" etc. ?
Gil