- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Script Problem - should be a simple one.
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
тАО04-01-2003 11:11 AM
тАО04-01-2003 11:11 AM
I seem to have gone brain dead early this week. I???m trying to write a quick script that pulls all the occurences of a word or number from a file. Then extract the first word or number from that to echo along with the count.
.
so far, I find collect the occurrences by
somenum=$(cat basefile | grep ???keyword??? | awk ???{print $5}???)
to load the word or number into ???somenum???.
.
Then, to find the number of times the target word represented by $5 is
found, I use
another=$(cat basefile|grep ???keyword???|wc ???l)
because the target word is only on the same line as the keyword.
.
Now, I need to pull the first word or number from ???somenum??? so I can have a ----
echo ???
.
The $another, of course is the count and
.
I can???t remember a way to pull the first occurrence of the target word from somenum.
.
Any help is greatly appreciated. I???m very generous with points.
.
Paul Middleton
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-01-2003 11:44 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-01-2003 12:08 PM
тАО04-01-2003 12:08 PM
Re: Script Problem - should be a simple one.
>>>>>>>>>>>>>>>>>>>>
#!/usr/bin/ksh
countwords() {
count=0
for word in `cat $2`
do
if [[ $word = $1 ]]; then
count=`expr $count +1`
fi
done
echo $count
}
echo "the word $1 occurs `countwords $1 $2` times in file $2"
exit 0
<<<<<<<<<<<<<<<<<<<<<
Call as follows -
count.sh
grep is only going to return the number of lines with (any number of occurences) of the word. so although it would be a pain to check each and every word and arrive at the count, unless you use perl, it isn't going to be easy...
and i don't know perl very well either. ;)
- ramd.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-01-2003 12:28 PM
тАО04-01-2003 12:28 PM
Re: Script Problem - should be a simple one.
for word in `cat $2`
may be changed to
for word in `cat $2 | grep $1`
May be a good cut in iterations where there a lot of lines containing zero occurences of the word.
- ramd.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-01-2003 01:24 PM
тАО04-01-2003 01:24 PM
Re: Script Problem - should be a simple one.
.
ramd - You gave me an idea for another project. I can use your input for reviewing older files on our systems. Currently the script is just for new files being ftp'd in. Now I can do both old and new a lot easier.
.
Thanks to both of you for your quick response.
.
Paul Middleton
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-01-2003 01:27 PM
тАО04-01-2003 01:27 PM
Re: Script Problem - should be a simple one.
#convert all uppercase to lowercase, use depending if you want to ignore capitialization
tr -cs "[a-z'0-9]" "\12" |
# replace all characters not a-z, ', or 0-9 with a newline, ie one word per line
sort|
#uniq expects sorted input
uniq -c |
#count number of times each word appears
sort +0nr +1d
# sort first from most to least frequent then alphabetically, or just grep for you word, grep $yourword
the quick and dirty is
num=$(<$yourfile |
tr -cs "[a-zA-Z0-9]" "\12" |
grep -xc $yourword |)
print "$yourword occurs $num times."
adjust your tr to include capitialization,digits,or punctuation that is allowed.