The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > OS Specific Forums > Linux
Google UNIX.COM


Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find a count of a word within a file bd_joy Shell Programming and Scripting 9 07-14-2008 06:29 AM
Count strings on single line? cs03dmj Shell Programming and Scripting 6 05-22-2008 01:09 AM
Problem to search multiple strings sudhish Shell Programming and Scripting 2 10-25-2007 11:00 AM
To find the count of records from tables present inside a file. navojit dutta Shell Programming and Scripting 1 08-29-2007 08:04 AM
find multiple file types and tar manthasirisha Shell Programming and Scripting 9 03-21-2006 09:37 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-23-2007
salaathi's Avatar
Registered User
 

Join Date: Sep 2007
Location: Madurai
Posts: 20
Question To find multiple strings count in a file

I need to find the line count of multiple strings in a particular file. The strings are as follows:

bmgcc
bmgccftp
bsmsftp
bulkftp
cctuneftp
crbtftp
crmpos
cso
gujhr

I am doing manual grep for each of the string to find the line count. The command i am using right now is:
grep mark push.cdr.20071123.144325 | wc -l

Is there any better way of doing this.


Thanks
Reply With Quote
Forum Sponsor
  #2  
Old 11-23-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Code:
awk '{ arr[$0]++ }END{for ( i in arr ) { print i, arr[i] } }' file
Reply With Quote
  #3  
Old 11-26-2007
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 556
Hi, salaathi.

You didn't say anything about the input file. Here's a script that allows a general input file. The file is broken apart, sorted, counted, and searched according to your list:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate counting strings in arbitrary text file.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash tr sort uniq grep

echo

FILE=data1
STR=strings1

echo " Input file:"
cat data1

echo
echo " Results of carving, sorting, counting, searching pipeline:"
tr ' ' '\n' <$FILE |
sort |
uniq -c |
grep -f $STR

exit 0
The input file has a number of your words scattered through a file of randomly generated strings. The script then produces:
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
tr (coreutils) 5.2.1
sort (coreutils) 5.2.1
uniq (coreutils) 5.2.1
grep (GNU grep) 2.5.1

 Input file:
Ekinsud myel irradiate persona chufte spumsuds diagnosable
esplanade dregh.  Minoan victor ighent intellectual otem
ensodsil.  Swibsik exclusion cereal transpacific loin florican
flagellate charm snick.  bulkftp cctuneftp cso bulkftp Adoptive
launch marrow gridiron heredity sulfa cultivable mutate sterile
partisan chamois.  Testamentary miraculous otsonsev silverware
cso bulkftp cctuneftpd meridian?  Scev quarterback hare hood
spectral adrift bourbon bulkftp cctuneftp cso cctuneftp
employed.  Doctrine win benevolent ethic plumbago trailhead men
Mbabane diagnoses incorrect Claire.  Assess yoke summate variate
Freddy fluid Rwanda confine Albany southbound origin.  Keel
Chapman prep blueback thirteen Halpern.  Piss affricate AZ
fanfold Lebanese rollick topocentric butte honorific pretext cso
cso bedevil?  Ers douce Wilhelm deniable seater scherzo.

 Results of carving, sorting, counting, searching pipeline:
      4 bulkftp
      3 cctuneftp
      1 cctuneftpd
      5 cso
If your input file does not need to be broken up, then you can eliminate the first step, the tr. See man pages for details ... cheers, drl
Reply With Quote
  #4  
Old 11-28-2007
salaathi's Avatar
Registered User
 

Join Date: Sep 2007
Location: Madurai
Posts: 20
Thumbs up

The same i tried with some for statement.
It's working fine.

Thanks for your response.

FILENAME=
string=
echo "enter file name :\c"
read FILENAME
echo "enter string to find seperated by space:\c"
read string
for rc in $string
do
grep $rc $FILENAME | wc -l
done
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0