hoge.txt
hogefugapiyohogefugapiyo
hogehogehogehogehogehoge
When you want to know the number of "hoge" from a file like this. (By the way, 8)
I tried to find out the number of occurrences with the built-in command, but it didn't work, so I wrote a script in Python. (Because grep may not be able to handle multiple occurrences in one line)
match_count.py
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import sys
import os.path
def clean_args(args):
if len(args) == 2:
search_word = args[1]
return (True, None, search_word)
if len(args) != 3:
print "[Usage] match_count.py $filename $search_word"
return (False, None, None)
target_file_path = args[1]
search_word = args[2]
if not os.path.exists(target_file_path):
print "[Error] File is not exist."
return (False, None, None)
return (True, target_file_path, search_word)
def count_words(filename, search_word):
if filename is not None:
# python 2.Because it was 4, I can't use with
stream = open(filename, 'r')
counter = _count(stream, search_word)
stream.close()
return counter
else :
return _count(sys.stdin, search_word)
def _count(stream, search_word):
counter = 0
for line in stream:
counter += line.count(search_word)
return counter
def main():
args = sys.argv
(is_valid, filename, search_word) = clean_args(args)
if not is_valid:
sys.exit()
print count_words(filename, search_word)
if __name__ == '__main__':
main()
Create this file on Linux and give it execute permission.
$ ./match_count.py hoge.txt hoge
8
You can get the number that matches hoge like this.
I also tried to support pipes
$ cat hoge.txt | ./match_count.py hoge
8
You can use it even if you like. I wonder if it will be better if I cat multiple files.
Recommended Posts