In python, this would do the job:
#!/usr/bin/env python3
s = """How to get This line that this word repeated 3 times in THIS line?
But not this line which is THIS word repeated 2 times.
And I will get This line with this here and This one
A test line with four this and This another THIS and last this"""
for line in s.splitlines():
if line.lower().count("this") == 3:
print(line)
outputs:
How to get This line that this word repeated 3 times in THIS line?
And I will get This line with this here and This one
Or to read in from a file, with the file as argument:
#!/usr/bin/env python3
import sys
file = sys.argv[1]
with open(file) as src:
lines = [line.strip() for line in src.readlines()]
for line in lines:
if line.lower().count("this") == 3:
print(line)
Paste the script into an empty file, save it as find_3.py
, run it by the command:
python3 /path/to/find_3.py <file_withlines>
Of course the word "this" can be replaced by any other word (or other string or line section), and the number of occurrences per line can be set to any other value in the line:
if line.lower().count("this") == 3:
Edit
If the file would be large (hundreds of thousands / millions of lines), the code below would be faster; it reads the file per line instead of loading the file at once:
#!/usr/bin/env python3
import sys
file = sys.argv[1]
with open(file) as src:
for line in src:
if line.lower().count("this") == 3:
print(line.strip())
$RANDOM_LANGUAGE
- somebody will be able to come up with a solution in it.