egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Grep Count Feature (Somewhat related to eggdrop)

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Modules & Programming
View previous topic :: View next topic  
Author Message
Vexor
Voice


Joined: 21 Jul 2006
Posts: 18
Location: Washington Court House

PostPosted: Tue Jan 06, 2009 9:21 pm    Post subject: Grep Count Feature (Somewhat related to eggdrop) Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
incith
Master


Joined: 23 Apr 2005
Posts: 275
Location: Canada

PostPosted: Tue Jan 06, 2009 11:02 pm    Post subject: Reply with quote

Interesting.
From a shell,
Quote:
$ 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.
_________________
; Answer a few unanswered posts!
Back to top
View user's profile Send private message
incith
Master


Joined: 23 Apr 2005
Posts: 275
Location: Canada

PostPosted: Tue Jan 06, 2009 11:03 pm    Post subject: Reply with quote

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.

Quote:
proc evnt:init_server {type} {


Quote:
$ 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 {

_________________
; Answer a few unanswered posts!
Back to top
View user's profile Send private message
Vexor
Voice


Joined: 21 Jul 2006
Posts: 18
Location: Washington Court House

PostPosted: Wed Jan 07, 2009 9:29 am    Post subject: Reply with quote

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:

#!/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
Back to top
View user's profile Send private message Visit poster's website
incith
Master


Joined: 23 Apr 2005
Posts: 275
Location: Canada

PostPosted: Wed Jan 07, 2009 11:50 pm    Post subject: Reply with quote

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:
% 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

_________________
; Answer a few unanswered posts!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Modules & Programming All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber