View previous topic :: View next topic |
Author |
Message |
Vexor Voice

Joined: 21 Jul 2006 Posts: 18 Location: Washington Court House
|
Posted: Tue Jan 06, 2009 9:21 pm Post subject: Grep Count Feature (Somewhat related to eggdrop) |
|
|
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 |
|
 |
incith Master

Joined: 23 Apr 2005 Posts: 275 Location: Canada
|
Posted: Tue Jan 06, 2009 11:02 pm Post subject: |
|
|
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 |
|
 |
incith Master

Joined: 23 Apr 2005 Posts: 275 Location: Canada
|
Posted: Tue Jan 06, 2009 11:03 pm Post subject: |
|
|
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 |
|
 |
Vexor Voice

Joined: 21 Jul 2006 Posts: 18 Location: Washington Court House
|
Posted: Wed Jan 07, 2009 9:29 am Post subject: |
|
|
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 |
|
 |
incith Master

Joined: 23 Apr 2005 Posts: 275 Location: Canada
|
Posted: Wed Jan 07, 2009 11:50 pm Post subject: |
|
|
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 |
|
 |
|