Search for strings with a certain number of letters, extensions included in grep

If I pipe ls- grephow can I return a list of files with the condition that they only have the number of letters x, the extension is added?

So, for example, if lsit gives me:

abcde.jav a156e.exc test.c prog1.c qwert r.c 

and I'm looking for all files containing strictly 5 letters, including extensions:

a156e.exc test.c prog1.c qwert

I tried:

ls | grep '^[a-z]${5}'
ls | grep "^[a-z]|[a-z]$"

and other similar things, but I can’t understand. It seems like the solution should be really simple, but I can't figure it out. Any help would be appreciated.

+4
source share
1 answer

You can use the following expression:

Living example

^([^a-z]*[a-z][^a-z]*){5}$

Explanation:

  • ^ - An anchor denoting the beginning of a line
  • ([^a-z]*[a-z][^a-z]*) - , ,
  • {5} - 5
  • $ - .

:

ls | grep -E '^([^a-z]*[a-z][^a-z]*){5}$'

ls:

for f in *; do
  if [[ $f =~ ^([^a-z]*[a-z][^a-z]*){5}$ ]]
    then
      echo $f
  fi
done
+3

Source: https://habr.com/ru/post/1628680/


All Articles