a06e
a06e

Reputation: 20774

Order filenames by number of appereances of given character?

I want to output a list of file names in a directory, where the list should be ordered descending by the number of appeareances of a given character in each file name. How can I do this with bash?

Upvotes: 0

Views: 38

Answers (1)

jyvet
jyvet

Reputation: 2191

Let say you want to sort by occurences of "a" in file names:

for i in *; do; echo "`grep -o "a" <<< "$i" | wc -l` $i"; done | sort -r

Result

$ ls
carla
elaine
guybrush
herman
largo
leamon-head
lechuck
max
meathook
ozzie
sam
stan
voodoo

$ for i in *; do; echo "`grep -o "a" <<< "$i" | wc -l` $i"; done | sort -r
2 leamon-head
2 carla
1 stan
1 sam
1 meathook
1 max
1 largo
1 herman
1 elaine
0 voodoo
0 ozzie
0 lechuck
0 guybrush

Upvotes: 2

Related Questions