1

Just playing with regular expressions to learn. Why does it match the other files: errsort, pytest.py, etc.? On the second line adding a question mark on the end matched two more files. Tried grep basic expressions too.

Thanks!

$ ls -x | egrep -i "(aa)(dd)?(cc)?(dd)?((bb(ccdd)?(bb)?)?)|(ccdd)"
aa       aaaa       aabb     aabbaa     aabbbb    aabbccdd  aabbccddcc  
aabbddbb
aaccaa   aaccdd   aaddaa   aaddccddccdd  aaddee  errsort   pytest.py   TEST
$ ls -x | egrep -i "(aa)(dd)?(cc)?(dd)?((bb(ccdd)?(bb)?)?)|(ccdd)?"
aa       aaaa     aabb    aabbaa        aabbbb  aabbccdd  aabbccddcc  aabbddbb
aaccaa   aaccdd   aaddaa  aaddccddccdd  aaddee  errsort   pytest.py   TEST
test.sh  vimtest

$ bash --version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
$ grep --version
grep (GNU grep) 3.7
$ ls | egrep -i "(aa)(dd)?(cc)?(dd)?((bb(ccdd)?(bb)?)?)|(ccdd)"
aa
aaaa
aabb
aabbaa
aabbbb
aabbccdd
aabbccddcc
aabbddbb
aaccaa
aaccdd
aaddaa
aaddccddccdd
aaddee
$ ls -x | egrep -i "(aa)(dd)?(cc)?(dd)?((bb(ccdd)?(bb)?)?)|(ccdd)?"
aa   aaaa     aabb    aabbaa    aabbbb  aabbccdd  aabbccddcc  aabbddbb
aaccaa   aaccdd   aaddaa  aaddccddccdd  aaddee  errsort   pytest.py   TEST
test.sh  vimtest
$ ls | egrep -i "(aa)(dd)?(cc)?(dd)?((bb(ccdd)?(bb)?)?)|(ccdd)?"
aa
aaaa
aabb
aabbaa
aabbbb
aabbccdd
aabbccddcc
aabbddbb
aaccaa
aaccdd
aaddaa
aaddccddccdd
aaddee
errsort
pytest.py
TEST
test.sh
vimtest
$ 

$ ls | egrep -io "(aa)(dd)?(cc)?(dd)?((bb(ccdd)?(bb)?)?)|(ccdd)?"
aa
aa
aa
aabb
aabb
aa
aabbbb
aabbccdd
aabbccdd
aabb
aacc
aa
aaccdd
aadd
aa
aaddccdd
ccdd
aadd

This doesn't give you the color, but the bold is red on my screen to indicate the pattern match i think.
The files on the bottom don't have any red.
Removing -x did affect output on the first one. The -o helps a lot!

0

2 Answers 2

5

An optional match like (ccdd)? is exactly that: optional.

It could happen that the matched text is empty, but still, that is a match, a line that has a match.

$ ls -x
aa   aaaa       aabb  aabbaa   aabbbb   aabbccdd  aabbccddcc  aabbddbb  aaccaa  aaccdd  aaddaa  aaddccddccdd  aaddee
errsort  pytest.py  TEST  test.sh  vimtest

$ ls -x | cat
aa   aaaa     aabb    aabbaa    aabbbb  aabbccdd  aabbccddcc  aabbddbb
aaccaa   aaccdd   aaddaa  aaddccddccdd  aaddee  errsort   pytest.py   TEST
test.sh  vimtest

It is important to understand that when the ls output is piped, it changes. Now there are three lines to match with the grep.

As the first and second lines match on the (several) aa, both are printed.

$ ls -x | egrep -i "(aa)(dd)?(cc)?(dd)?((bb(ccdd)?(bb)?)?)|(ccdd)"
aa   aaaa     aabb    aabbaa    aabbbb  aabbccdd  aabbccddcc  aabbddbb
aaccaa   aaccdd   aaddaa  aaddccddccdd  aaddee  errsort   pytest.py   TEST

$ ls -x | egrep -i "(aa)(dd)?(cc)?(dd)?((bb(ccdd)?(bb)?)?)|(ccdd)?"
aa   aaaa     aabb    aabbaa    aabbbb  aabbccdd  aabbccddcc  aabbddbb
aaccaa   aaccdd   aaddaa  aaddccddccdd  aaddee  errsort   pytest.py   TEST
test.sh  vimtest

As shown, the third line gets a match on the empty string due to the (ccdd)?.

I am assuming the format of your list of files is incorrect.

2
  • It was matching those even with LS input from file (one file per line (ls > inputFile)). It is because of the hanging |(ccdd)? catching all matches. I thought the starting aa would stop that. When I grouped the last |(ccdd) into parentheses with the previous group all output was as expected.
    – kserwick
    Commented Aug 29, 2022 at 14:47
  • 1
    You are using ...|(ccdd)? which means "something" or the optional (ccdd), which does match "anything", yes, To have the aa avoid that you need something similar to aa(...|(ccdd)?). @kserwick Commented Aug 29, 2022 at 18:38
1

grep isn't matching the files like errsort .... it's matching the line, and the only thing it actually matches is the (aa) (everything else is optional because of the ?). Because you chose ls -x the output of ls is one long line and the aa matches.

Why the non-matching alternativ group shows more files I can't understand, maybe a bug in egrep.

You must log in to answer this question.

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