The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep all records in a file and get a word count -perl meghana Shell Programming and Scripting 4 02-13-2008 10:06 PM
find a word in a file, and change a word beneath it ?? vikas027 Shell Programming and Scripting 2 02-13-2008 04:23 PM
To find multiple strings count in a file salaathi Linux 3 11-28-2007 06:31 AM
Count the number of occurence of perticular word from file rinku Shell Programming and Scripting 40 08-10-2007 07:33 PM
how to find a word repeated in a file gurukottur UNIX for Dummies Questions & Answers 4 08-24-2006 04:53 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-02-2008
bd_joy bd_joy is offline
Registered User
  
 

Join Date: Aug 2005
Location: Pittsburgh, PA
Posts: 25
How to find a count of a word within a file

Hello,
I'm looking for a wait to count the number of occurrences of a certain string of characters within a file. The file that I trying to parce has segments within the file that have a header and footer to each segment and I'm trying to do a count of the header string and compare it to a count of the footer string to verify the integrity of the file.

Example file:
HEADER misc text and numeric values FOOTER HEADER more misc info FOOTER HEADER etc etc FOOTER

There are no carriage returns within the file, and it's about 50 to 60MB text file so the process needs to be somewhat efficient as a short processing timeframe is required.


I've done several searches and tried using wc, tr, and sort is a variety of different ways but I'm no closer to finding a solution. I'm a novice to the use of utilities like sed or awk, but ideas using them are welcome.

Other general info: I use ksh on AIX 5.3. Thanks for any help!
  #2 (permalink)  
Old 05-02-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
You can try something like this, it prints the numbers of the words HEADER and FOOTER:

Code:
awk 'BEGIN{RS=" "}/HEADER/{h++}/FOOTER/{f++}END{print h, f}' file
Regards
  #3 (permalink)  
Old 05-02-2008
bd_joy bd_joy is offline
Registered User
  
 

Join Date: Aug 2005
Location: Pittsburgh, PA
Posts: 25
Thanks for the reply! I'll test any post the results.
  #4 (permalink)  
Old 05-02-2008
in2nix4life's Avatar
in2nix4life in2nix4life is offline
Registered User
  
 

Join Date: Oct 2007
Location: East Coast
Posts: 58
Here's another way:

#!/bin/ksh
str1=`cat file.txt | tr ' ' '\n' | grep "HEADER" | wc -l | sed 's/^[ \s]*//'`
str2=`cat file.txt | tr ' ' '\n' | grep "FOOTER" | wc -l | sed 's/^[ \s]*//'`

print "$str1 instances of 'HEADER' found!"
print "$str2 instances of 'FOOTER' found!"

exit 0

Hope this helps.
  #5 (permalink)  
Old 05-02-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
$ echo 'HEADER misc text and numeric values FOOTER HEADER more misc info FOOTER HEADER etc etc FOOTER' | nawk -F'(HEADER)|(FOOTER)' '{print "header+footer-> " NF-1}'
header+footer-> 6
If header/footer come in 'pairs', the odd number would constitute the misconfiguration.
  #6 (permalink)  
Old 07-14-2008
DrZoidberg DrZoidberg is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 6
I've got a follow up bump to add to this. I need to count the number of rows that start with the number 10. I can't count the instances because 10 often appears in the rows as well.

I'd be very grateful for some help on this.
  #7 (permalink)  
Old 07-14-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink just use grep for that

Code:
cat file1 | grep "^10 " | wc -l
will return number of lines that begin with a 10 (and not 100)
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0