This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Grep Count Feature (Somewhat related to eggdrop)

Discussion of Eggdrop's code and module programming in C.
Post Reply
User avatar
Vexor
Voice
Posts: 18
Joined: Fri Jul 21, 2006 4:22 am
Location: Washington Court House
Contact:

Grep Count Feature (Somewhat related to eggdrop)

Post by Vexor »

Hey guys, I was playing around with grep earlier and was doing some bracket counting and came across this odd little quirk.

[squib@amish eggdrop]$ grep -c '{' eggdrop.conf
15
[squib@amish eggdrop]$ grep -c '}' eggdrop.conf
16

Now I have gone through and manually counted the brackets in eggdrop.conf, they match up. The eggdrop doesn't send out any errors and in fact runs smoothly.

So I'm just wondering, is this a bug with grep or is there something I'm completely missing?
"just the usual suggestion, RTFM" --demond
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Post by incith »

Interesting.
From a shell,
$ grep -c '{' visitant.conf
11
But there are actually 12 { in my conf file... Tcl would complain I'm sure if they didn't actually match up.
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Post by incith »

Ah I understand, it's counting the number of line-matches the way I see it, not character matches. } is on 12 lines whereas { is on 11 lines.
proc evnt:init_server {type} {
$ grep '{' visitant.conf
set altnick "${nick}-??"
set username "${nick}"
set botnet-nick "${nick}"
set notefile "${nick}.notes"
set userfile "${nick}.user"
set pidfile "${nick}.pid"
set chanfile "${nick}.chan"
set global-chanset {
proc evnt:init_server {type} {
putserv {}
set servers {
User avatar
Vexor
Voice
Posts: 18
Joined: Fri Jul 21, 2006 4:22 am
Location: Washington Court House
Contact:

Post by Vexor »

Ah I see. Confirmed in a test file.

Is there any way to make grep count the actual times a { or } appears in a file? It's been over a year since I've even touched a linux command.

*EDIT*
Ah, with a little bit of help from my old pal google, I came across this:

http://codesnippets.joyent.com/tag/count

perl -lne '$c++ while /STRING_TO_COUNT/g; END { print $c; }'

Example: cat eggdrop.conf | perl -lne '$c++ while /}/g; END { print $c; }'
Result: 16
cat eggdrop.conf | perl -lne '$c++ while /{/g; END { print $c; }'
16

So for anyone out there who's missing a " or {} [] etc...
cat filename | perl -lne '$c++ while /WHAT_YOURE_COUNTING_FOR/g; END { print $c; }'

*FURTHER EDIT*

Here is a simple bash script you can put in place to do this from one command:

Code: Select all

#!/bin/bash
cat $1 | perl -lne '$c++ while /'$2'/g; END { print $c; }'
Example: count test {

Just pop that into a file aptly named (I used count) and place in your /bin. Of course don't forget to chmod!
"just the usual suggestion, RTFM" --demond
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Post by incith »

Er well I guess I should have posted a solution for you.

Yes, perl is one choice, although it will not be cross platform.

In Tcl..:

Code: Select all

% set confd [open eggdrop/visitant.conf]
file3
% while {![eof $confd]} {
  append conf [gets $confd]
}
% regexp "{" $conf
1
% regexp -all "{" $conf
12
% regexp -all "}" $conf
12
Post Reply