user10547478
user10547478

Reputation:

Grep - how to find lines with at least 3 times a specific number

I have a file filled with binary strings. Now I need to find the lines with at least three times the number 1 in it. How do I do this using grep?

Upvotes: 0

Views: 1258

Answers (2)

TommyD
TommyD

Reputation: 781

This is easy:

grep '1.*1.*1' file

'.*' means any character any number of times including no character. The expression will match no matter how many characters in between, before or after the ones, but will need to have three ones to match.

Upvotes: 0

Tom Fenech
Tom Fenech

Reputation: 74685

Match 1 followed by anything (including an empty string), 3 times:

grep -E '(1.*){3}' file

-E enables Extended regex, otherwise you could use:

grep '\(1.*\)\{3\}' file

Upvotes: 2

Related Questions