- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Counting number of occurrences 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
Discussions
Discussions
Forums
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
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
тАО05-09-2002 01:02 AM
тАО05-09-2002 01:02 AM
I need to count the number of occurrences of a character in a file. Is there a Shell command to do it?
Regards,
Ram.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2002 01:11 AM
тАО05-09-2002 01:11 AM
Re: Counting number of occurrences of a character
awk -Fx '/x/{t+=NF-1}END{print t}' myfile
This works with blank lines, lines without any x and with something else, also lines with several occurrances on a single line.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2002 01:39 AM
тАО05-09-2002 01:39 AM
SolutionUsing perl is most straightforward.
If not, then you can resort to the many creative ways of extracting the occurrence count. Apart from using awk, you can also make use of tr:
# echo `cat $FILE` | tr [$CHAR] ["\n"] | wc -l
Just minus 1 from the result and you get the number of occurrences of the character.
This works because:
1) echo `cat $FILE` sets the entire file on a single line.
2) tr [$CHAR] ["\n"] adds a newline everytime it encounters the character (by converting it to a newline).
3) wc -l counts the number of newlines which is no. of character occurrences + 1
If you want this in a SHELL script, it will be as follows:
count.sh
=====================================
#!/sbin/sh
single=`cat $1`
lines=`echo $single | tr [$2] ["\n"] | wc -l`
echo "Total occurrences of $2 in $1: `expr $lines - 1`"
=====================================
To run the script:
# count.sh /etc/profile e
Hope this helps. Regards.
Steven Sim Kok Leong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2002 01:53 AM
тАО05-09-2002 01:53 AM
Re: Counting number of occurrences of a character
Try this script (this is to count the number of "c" character in a file, you can change it easy for your character:
numcar=`sed -e 's/[a-b]//g' -e 's/[d-z]//g' filename |wc -c `
numlin=`cat filename | wc -l `
integer numcarok
(( numcarok = numcar - numlin ))
echo $numcarok
Hope this help,
Justo.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2002 02:01 AM
тАО05-09-2002 02:01 AM
Re: Counting number of occurrences of a character
x
x
x
would return 5 or 6 instead of 3.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2002 02:02 AM
тАО05-09-2002 02:02 AM
Re: Counting number of occurrences of a character
You didn't read:
1) echo `cat $FILE` sets the entire file on a single line.
Hope this helps. Regards.
Steven Sim Kok Leong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2002 02:10 AM
тАО05-09-2002 02:10 AM
Re: Counting number of occurrences of a character
I believe your script works on the assumption that the file contains only alphabets, no numbers and no special characters such as the space character or the dollar sign etc.
Hope this helps. Regards.
Steven Sim Kok Leong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2002 02:18 AM
тАО05-09-2002 02:18 AM
Re: Counting number of occurrences of a character
Yes, your solution is better than mine.
Regards,
Justo.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2002 02:29 AM
тАО05-09-2002 02:29 AM
Re: Counting number of occurrences of a character
file 88:
hhohohohohhh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-09-2002 04:05 AM
тАО05-09-2002 04:05 AM
Re: Counting number of occurrences of a character
Regards,
Ram.