Command to have a count of matching string

ashsysad
ashsysad used Ask the Experts™
on
Hello,

I need a command-set or script in Perl which will take a String and File-name as inputs and displays the number of times the String is present in that file.  Using 'grep' command with -c option will display the number of matching lines in which the String is present but not the count.  Please let me know.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
sed 's/your string/&\n/g' your_file | grep -c 'your string'

Open in new window


This will cause that there will be only one 'your string' per line. Then "grep -c" will count these lines.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
is the number of matching lines in which the String is present different from the count because the string may occur more than once on a line?

If so, you might use something like

perl -lne 'BEGIN{$_=shift}END{print $.-1}' String File-name

(this assumes that the last String in the file is followed by a newline or some other character)

Author

Commented:
Hi Ozo,  I'm not getting the correct value with your syntax. Could you please check ?
The output should be 10 in the below example.


[root@iss-chi-rad01 ashok]# cat testfile
A disk "snapshot" is a copy of the virtual machine disk file (VMDK) at a certain point in time. It preserves the disk file system and  \n
system memory of your VM by enabling you to revert to the snapshot in case something goes wrong. Snapshot can be real lifesavers when \n
upgrading or patching applications and servers. This article will go over everything you need to know about using snapshot with VMware,  \n
including what they are, how they work and advanced techniques. A virtual machine provides several operations for creating and managing  \n
snapshots and snapshot chains. These operations let you create snapshots, revert to any snapshot in the chain, and remove snapshots. You  \n
can create extensive snapshot trees.
[root@iss-chi-rad01 ashok]# perl -lne 'BEGIN{$_=shift}END{print $.-1}' snapshot testfile
5
[root@iss-chi-rad01 ashok]#
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Top Expert 2007
Commented:
You can do it with a simple grep

grep -io snapshot testfile | wc -l

Open in new window

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
Sorry I don't know how
perl -lne 'BEGIN{$/=shift}END{print $.-1}'
got changed to
perl -lne 'BEGIN{$_=shift}END{print $.-1}'
but that will only count the 9 "snapshot"s not the "Snapshot"
If you want case independent matching, you might try
perl -lne 'BEGIN{$r=qr/\Q@{[shift]}/i}$c+=()=/$r/g;END{print $c}' snapshot testfile

Author

Commented:
Thanks Tintin. That works perfectly.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial