Linux – How to grep lines which have more than specific number of special characters

awklinuxregular expressionsed

i would like to know what's the best way to grep lines from text which have more than specific number of special characters.

Let's say that you already know that each line have 4 comma , and you would like to grep lines which have more than 4 comma ,

Example

hi,hello,how,are,you
catch,me,then,say,hello,then

Output

catch,me,then,say,hello,then

Best Answer

Perl solution:

perl -ne 'print if tr/,// > 4'
  • -n reads the file line by line
  • the tr operator returns the number of matches.

To print the lines with less than 4, just change > to <.

Related Question