To find multiple strings count in a file


 
Thread Tools Search this Thread
Operating Systems Linux To find multiple strings count in a file
# 1  
Old 11-23-2007
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
# 2  
Old 11-23-2007
Code:
awk '{ arr[$0]++ }END{for ( i in arr ) { print i, arr[i] } }' file

# 3  
Old 11-26-2007
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
# 4  
Old 11-28-2007
MySQL

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

2. Solaris

How to find multiple strings on different lines in file?

Hello, I have spent considerable amount of time breaking my head on this and reached out here. here is the back ground. OS - Solaris 10 There are two strings '<Orin>sop' and '<Dup>two' which I wanted to look for in a file without the quotes on different lines and ONLY if both strings are... (5 Replies)
Discussion started by: keithTait309875
5 Replies

3. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 Replies

4. Shell Programming and Scripting

Find multiple strings and replace single string

Hi, following Perl code i used for finding multiple strings and replace with single string. code: #!/usr/bin/perl my @files = <*.txt>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)'; my $replaceStr =... (2 Replies)
Discussion started by: chettyravi
2 Replies

5. Shell Programming and Scripting

How to find multiple strings within files under a directory

Hi guys, I need to find multiple strings within files under a directory and secondly, to count how many files are there with these strings. At present, i am able to do this in order to find one string (for example "abc"): find <path> -exec grep "abc" {} /dev/null \; Now for example, i... (2 Replies)
Discussion started by: frum
2 Replies

6. Shell Programming and Scripting

Find position of character in multiple strings in a file

Greetings. I have a file with information like this: AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU? AMNDHRKEEU?AMNDHREOEU? AMNDHREU?AHRKEOEU?AMNDHRKEU?AMNDKEOEU? What I need to extract is the position, in every line, of every occurrence of '?' A desired output would be something... (6 Replies)
Discussion started by: Twinklefingers
6 Replies

7. Programming

Perl:how to find multiple strings

Hi , i'm trying to find lines in a file that have 2 matches,but i have trouble with regex -it doesn't search for a second string-please help #!/usr/bin/perl -w use strict; use warnings; my $log = "log"; my $log_string = "grep '^8=' $log |"; usage (); chomp (my $TAB_1 = $ARGV); chomp (my... (2 Replies)
Discussion started by: auto_w
2 Replies

8. UNIX for Dummies Questions & Answers

how to find and replace strings in multiple files

Hi All, Iam new to unix, I need to find string and replace it in the file name. Like text_123_0.txt,text_123_1.txt,text_123_2.txt. I need to search 123 and replace it with 234 . Is there any unix command to replace them in single command since i have 5 directories. So i need to go each and every... (0 Replies)
Discussion started by: etldeveloper
0 Replies

9. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

10. Shell Programming and Scripting

Count occurance of multiple strings using grep command

How to grep multiple string occurance in input file using single grep command? I have below input file with many IDP, RRBE messages. Out put should have count of each messages. I have used below command but it is not working grep -cH "(sent IDP Request)(Recv RRBCSM)" *.txt ... (5 Replies)
Discussion started by: sushmab82
5 Replies
Login or Register to Ask a Question