Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep files containing two or more occurrence of a specific string

Tags:

grep

unix

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.

like image 961
Hubert Léveillé Gauvin Avatar asked May 30 '14 18:05

Hubert Léveillé Gauvin


People also ask

Can you grep two things at once?

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.

How do I grep for a string in multiple files in a directory?

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.

How find multiple occurrences of a string in Linux?

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.

How do you count occurrences with grep?

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.


1 Answers

What about this:

grep -o -c Hello * | awk -F: '{if ($2 > 1){print $1}}' 
like image 78
John C Avatar answered Sep 20 '22 23:09

John C