LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-02-2007, 10:08 PM   #1
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
command to count occurances of a string


I need a command that will count the occurances of the string "<FileDetails>" within a large text file. Anybody know if off the top of their heads?
 
Old 06-02-2007, 10:43 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Something like this should work ...

Code:
awk 'BEGIN{RS="$%^"}{for(i=1;i<=NF;i++){if($i ~ /<FileDetails>/){count++}}}END{print count}' file
Note that I've set the record-separator to a sequence of characters
not contained in my test-file.


Another approach:
Code:
grep -o '<FileDetails>' file |wc -l

Cheers,
Tink
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Count occurances of a string in a text file MicahCarrick Linux - Software 1 06-24-2006 04:25 PM
how many occurances of a substring are they n a string linuxmandrake Programming 1 04-11-2006 02:02 AM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM
command to count how many lines in a file? dr_zayus69 Linux - Software 3 01-06-2005 10:42 AM
Count of unread messages from command line rebuke Linux - Software 0 12-26-2003 06:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration