简体   繁体   中英

How do I count the number of occurrences of a string in an entire file?

Is there an inbuilt command to do this or has anyone had any luck with a script that does it?

I am looking to count the number of times a certain string (not word) appears in a file. This can include multiple occurrences per line so the count should count every occurrence not just count 1 for lines that have the string 2 or more times.

For example, with this sample file:

blah(*)wasp( *)jkdjs(*)kdfks(l*)ffks(dl
flksj(*)gjkd(*
)jfhk(*)fj (*) ks)(*gfjk(*)

If I am looking to count the occurrences of the string (*) I would expect the count to be 6, ie 2 from the first line, 1 from the second line and 3 from the third line. Note how the one across lines 2-3 does not count because there is a LF character separating them.

Update : great responses so far! Can I ask that the script handle the conversion of (*) to \\(*\\) , etc? That way I could just pass any desired string as an input parameter without worrying about what conversion needs to be done to it so it appears in the correct format.

您可以使用grepwc等基本工具:

grep -o '(\*)' input.txt | wc -l

Using perl's "Eskimo kiss" operator with the -n switch to print a total at the end. Use \\Q...\\E to ignore any meta characters.

perl -lnwe '$a+=()=/\Q(*)/g; }{ print $a;' file.txt

Script:

use strict;
use warnings;

my $count;
my $text = shift;

while (<>) {
    $count += () = /\Q$text/g;
}

print "$count\n";

Usage:

perl script.pl "(*)" file.txt 

This loops over the lines of the file, and on each line finds all occurrences of the string "(*)". Each time that string is found, $c is incremented. When there are no more lines to loop over, the value of $c is printed.

perl -ne'$c++ while /\\(\\*\\)/g;END{print"$c\\n"}' filename.txt

Update: Regarding your comment asking that this be converted into a solution that accepts a regex as an argument, you might do it like this:

perl -ne'BEGIN{$re=shift;}$c++ while /\\Q$re/g;END{print"$c\\n"}' 'regex' filename.txt

That ought to do the trick. If I felt inclined to skim through perlrun again I might see a more elegant solution, but this should work.

You could also eliminate the explicit inner while loop in favor of an implicit one by providing list context to the regexp:

perl -ne'BEGIN{$re=shift}$c+=()=/\\Q$re/g;END{print"$c\\n"}' 'regex' filename.txt

You can use basic grep command:

Example : If you want to find the no of occurrence of "hello" word in a file

grep -c "hello" filename

If you want to find the no of occurrence of a pattern then

grep -c -P "Your Pattern"

Pattern example : hell.w, \\d+ etc

I have used below command to find particular string count in a file

grep search_String fileName|wc -l

text="(\*)"
grep -o $text file | wc -l

You can make it into a script which accepts arguments like this:

script count :

#!/bin/bash
text="$1"
file="$2"
grep -o "$text" "$file" | wc -l

Usage:

./count "(\*)" file_path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM