I want to search a file to check for patterns like this: text1,text2,[1.2.3,3.4.5,6.7.8,etc] exist. The number of commas inside the square brackets is undefined.

I tried the following command but it prints the commas after text1 and text2 which is not what I want. I'm looking only for finding commas inside the square brackets.

$ grep -E '[*,*]' myfile.txt


Here's some sample input. Note that the commas inside brackets can be more or less than the number in the example (i.e. undefined number so I have to capture them with regex):

1.com,,txt1,txt11,['', '']
3.com,,txt3,txt31,['', '', '']

1 Answer 1


To match lines that contain at least one comma inside square brackets, you can use

grep '\[.*,.*\]'


$ grep '\[.*,.*\]' file
1.com,,txt1,txt11,['', '']
3.com,,txt3,txt31,['', '', '']

The square brackets need to be escaped because they have special meaning in regular expression syntax. Notice I didn't need to use -E with grep, the basic pattern matching facilities of grep were sufficient.

NOTE: grep will typically display the matches as colored so you can tell what's actually getting matched by any patterns you use:


  • The expression would also match [''],[''], but this was not part of the test cases in the question.
    – Kusalananda
    Aug 12, 2018 at 14:38
  • @Kusalananda good point - I guess \[[^]]*,[^[]*\] would eliminate that case? Aug 12, 2018 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.