If I pipe ls
to grep
, how would I be able to return a list of files with the condition that they only have an x amount of letters, extension included?
So for example, if ls
gives me:
abcde.jav a156e.exc test.c prog1.c qwert r.c
and I'm looking for all files that contain strictly 5 letters, extensions included:
a156e.exc test.c prog1.c qwert
I've tried:
ls | grep '^[a-z]${5}'
ls | grep "^[a-z]|[a-z]$"
and other things like that, but I can't seem to get it. It seems like the solution should be really simple but I can't figure it out. Any help would be appreciated.
You could use the following expression:
^([^a-z]*[a-z][^a-z]*){5}$
Explanation:
^
- Anchor denoting the start of the string([^a-z]*[a-z][^a-z]*)
- Group that matches a single letter between zero or more non-letter characters{5}
- Match the previous group 5 times$
- Anchor denoting the end of the string.Usage:
ls | grep -E '^([^a-z]*[a-z][^a-z]*){5}$'
Or without ls
:
for f in *; do
if [[ $f =~ ^([^a-z]*[a-z][^a-z]*){5}$ ]]
then
echo $f
fi
done