- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- script to count the no. of a particular word
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-03-2007 08:21 PM
тАО05-03-2007 08:21 PM
script to count the no. of a particular word
I tried doing this using "wc" and "grep" commands but was unsuccessful.
Please help me out.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-03-2007 09:04 PM
тАО05-03-2007 09:04 PM
Re: script to count the no. of a particular word
Post the code you are using please.
grep swinstall * | wc -l
That will get you close.
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
тАО05-03-2007 09:18 PM
тАО05-03-2007 09:18 PM
Re: script to count the no. of a particular word
if you have one occurence per line,
you can also try:
awk '/swinstall/{n++}; END {print n+0}' *.txt
hope this helps too!
kind regards
yogeeraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-03-2007 09:20 PM
тАО05-03-2007 09:20 PM
Re: script to count the no. of a particular word
This will not serve the purpose. Suppose if there are 3 words by name swinstall in a line that will be counted as one. I want those 3 words in a single line to the counted as 3.
Below is the code that I used
for i in *
do
count=`grep -c swinstall $i`
echo $i "has " $count "swinstalls"
done
This code also has the same problem as the code you have given
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-03-2007 09:24 PM
тАО05-03-2007 09:24 PM
Re: script to count the no. of a particular word
Suppose the following are the contents of the file
---------------------------------------------
swinstall
swinstall use
i am called swinstall
swinstall is called swinstall everywhere else
---------------------------------------------
You awk script will count the no. of occurences of swinstall as 4, whereas the answer I am expecting is 5. The last line has 2 occurences of swinstall. This is counted as just one.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-03-2007 10:00 PM
тАО05-03-2007 10:00 PM
Re: script to count the no. of a particular word
# sed -n 's/\(\
Cheers,
Wout
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-03-2007 10:03 PM
тАО05-03-2007 10:03 PM
Re: script to count the no. of a particular word
The sed command will just look for any occurence of 'swinstall' and put each occurence on a line of its own (adding linebreaks before and after). Then, it becomes possible to grep for lines which hold only this word and count them.
Cheers,
Wout
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-03-2007 10:31 PM
тАО05-03-2007 10:31 PM
Re: script to count the no. of a particular word
I guess it's a start, whatever your ultimate plan is.
#!/bin/sh
# Will count the number of occurences of MYSTRING in the files within MYDIR
#
MYDIR="/root/tmp"
MYSTRING="swinstall"
for i in `ls $MYDIR`
do
echo $i has `sed -n "s/\(\<${MYSTRING}\>\)/\n\1\n/gp" $i | grep "^${MYSTRING}$" | wc -l` occurences of $MYSTRING
done
exit 0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-03-2007 10:36 PM
тАО05-03-2007 10:36 PM
Re: script to count the no. of a particular word
To count the number of occurancs of a pattern ("word") you can use:
# perl -lne 'BEGIN{$count=0};$count++ while (m/\bswinstall\b/ig);END{print $count}' file
This counts the total number of occurances of the string "swinstall" in 'file'.
You can total the count for any number of files by passing multilple file arguments.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-04-2007 12:43 AM
тАО05-04-2007 12:43 AM
Re: script to count the no. of a particular word
try the attached script for a start: $1="string to search for", $2="file to search".
The count is faked, but it may produce the correct result.
regards,
John K.