I need to find files where a specific string appears twice or more.
For example, for three files:
File 1:
Hello World!
File 2:
Hello World! Hello !
File 3:
Hello World! Hello Hello Again.
--
I want to grep Hello
and only get files 2
& 3
.
Grep is a powerful utility available by default on UNIX-based systems. The name stands for Global Regular Expression Print. By using the grep command, you can customize how the tool searches for a pattern or multiple patterns in this case. You can grep multiple strings in different files and directories.
To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.
Place your caret on the word or text range you want to find and select multiple occurrences of and then use ⌃G (macOS), Alt+J (Windows/Linux) to find and select the next occurrence of the word. This is a case-sensitive search.
Using grep -c alone will count the number of lines that contain the matching word instead of the number of total matches. The -o option is what tells grep to output each match in a unique line and then wc -l tells wc to count the number of lines. This is how the total number of matching words is deduced.
What about this:
grep -o -c Hello * | awk -F: '{if ($2 > 1){print $1}}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With