1

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

EDIT #1

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,1.2.3.4,txt1,txt11,['1.2.3.4', '5.6.7.8']
2.com,3.4.5.6,txt2,txt21,['5.6.6.6']
3.com,5.5.5.5,txt3,txt31,['1.1.1.1', '2.2.2.2', '3.3.3.3']
0

1 Answer 1

1

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

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

Ex.

$ grep '\[.*,.*\]' file
1.com,1.2.3.4,txt1,txt11,['1.2.3.4', '5.6.7.8']
3.com,5.5.5.5,txt3,txt31,['1.1.1.1', '2.2.2.2', '3.3.3.3']

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:

   ss1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .