Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to count the number of times a string appears in a file

Status
Not open for further replies.

CaseyR

Technical User
Oct 23, 2008
3
US
Hi, just getting restarted in programming from simple scripts. Using perl, how do I count the number of occurences of a string within a file? I can get my snipits to print the occurences, but what would be the simplest way to store them for a count, or just get perl to count them? I can use a line count, but then I have to create a temp file. Any advice is helpful, like I said, it's been a while and I'm just getting started again.

Thanks
 
The perl FAQs are full of helpful bits of information. Since it is not clear what you are counting take a look at this FAQ and see if you can apply the logic to your particular requirements.


Or post a sample of your data and show what you need to count.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Here's some additional information, I have a sample of the file output I'm working with. I need to specifically count the number of FastEthernet ports that have the string No traffic sent or recieved, and the ones that are disabled, but not the Gigabit ports. Sample includes the header data just as refrence.
Sample text from file:

Results for Device switchname
Device Deployed
Pre Device Execution Message:
Post Device Execution Message:
Update Startup Not Attempted
Results of Deploy
Deploy Message: Deploy successful (Primary Login Succeeded
/ Primary Enable Succeeded
)

Show clock
21:22:35.441 UTC Wed Oct 15 2008
switchname#show interface accounting
Interface Vlan1 is disabled

Vlan128
Protocol Pkts In Chars In Pkts Out Chars Out
IP 537943 73938129 274898 40796788
ARP 418327 25099620 237 14220
FastEthernet0/1
Protocol Pkts In Chars In Pkts Out Chars Out
Other 0 0 218587 13115220
Spanning Tree 0 0 29 1856
CDP 0 0 36482 15103548
FastEthernet0/2
Protocol Pkts In Chars In Pkts Out Chars Out
Other 0 0 218592 13115520
Spanning Tree 0 0 13 832
CDP 0 0 36451 15090714
FastEthernet0/3
Protocol Pkts In Chars In Pkts Out Chars Out
Other 0 0 218582 13114920
Spanning Tree 0 0 12 768
CDP 0 0 36494 15108516
FastEthernet0/4
Protocol Pkts In Chars In Pkts Out Chars Out
Other 0 0 218589 13115340
Spanning Tree 0 0 12 768
CDP 0 0 36464 15096096
FastEthernet0/5
Protocol Pkts In Chars In Pkts Out Chars Out
Other 0 0 218584 13115040
Spanning Tree 0 0 12 768
CDP 0 0 36526 15121764
FastEthernet0/6
Protocol Pkts In Chars In Pkts Out Chars Out
No traffic sent or received on this interface.
FastEthernet0/48
Protocol Pkts In Chars In Pkts Out Chars Out
No traffic sent or received on this interface.
GigabitEthernet0/1 connection to DJJ-114-SWI-3
Protocol Pkts In Chars In Pkts Out Chars Out
Other 0 0 72890 4373400
Spanning Tree 4363550 274903664 9 540
CDP 36451 14944910 36451 14944910
DTP 72890 4373400 0 0
GigabitEthernet0/2
Protocol Pkts In Chars In Pkts Out Chars Out
No traffic sent or received on this interface.
GigabitEthernet0/3
Protocol Pkts In Chars In Pkts Out Chars Out
No traffic sent or received on this interface.
GigabitEthernet0/4
Protocol Pkts In Chars In Pkts Out Chars Out
No traffic sent or received on this interface.
 
That is a bit different than your original post. You're not simply counting the occurance of a string in the lines of a file. You're looking for one thing and counting something else .

Is this school work of some sort?





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
No just trying help out my co workers in networks. Most of the scripts I have dealy with so far were much simpler, with the exception of NMIS, but they are not using SNMP.
 
Here is one possibility:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$faste[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[black][b]my[/b][/black] [blue]$count[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]IN, [red]'[/red][purple]path/to/datafile[/purple][red]'[/red][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]
[olive][b]while[/b][/olive][red]([/red]<IN>[red])[/red][red]{[/red]
   [olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]^FastEthernet[/purple][red]/[/red][red])[/red] [red]{[/red]
      [blue]$faste[/blue] = [fuchsia]1[/fuchsia][red];[/red]
   [red]}[/red]
   [olive][b]elsif[/b][/olive] [red]([/red][red]/[/red][purple]^GigabitEthernet[/purple][red]/[/red][red])[/red] [red]{[/red]
      [blue]$faste[/blue] = [fuchsia]0[/fuchsia][red];[/red]
   [red]}[/red]
   [olive][b]elsif[/b][/olive] [red]([/red][blue]$faste[/blue][red])[/red] [red]{[/red]
      [blue]$count[/blue]++ [olive][b]if[/b][/olive] [red]([/red][blue]$_[/blue] eq [red]"[/red][purple]No traffic sent or received on this interface.[purple][b]\n[/b][/purple][/purple][red]"[/red][red])[/red][red];[/red]
      [blue]$count[/blue]++ [olive][b]if[/b][/olive] [red]([/red][blue]$_[/blue] eq [red]"[/red][purple]Line that has the disabled message.[purple][b]\n[/b][/purple][/purple][red]"[/red][red])[/red][red];[/red]
   [red]}[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url] IN[red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top