0

I want something like this:

find "$dir" -type f -printf '%i:<alpha_char_count_of_base_filename>:%d:%p\n'

Then I want to sort first on inode number, then on alpha_char_count_of_base_filename. I think I have part of the solution figured out.

I know I can get the alpha_char_count_of_base_filename with this code:

basename "$filename" | tr -cd [:alpha:] | wc -m

However, trying something like this (don't laugh) doesn't work:

find "$dir" -type f -printf '%i:$(basename -print | tr -cd [:alpha:] | wc -m):%d:%p\n'

I've tried a lot of ideas and I have not lucked onto a working solution. Here's another idea I tried:

find "$dir" -type f -printf '%i:%d:%p\n' | awk 'BEGIN{FS=":";OFS=":";} {print $1,system("basename "$3" | tr -cd [:alpha:] | wc -m"),$2,$3"\n";}'

This seems to be getting me closer, but it prints the alpha_char_count_of_base_filename in the wrong location. I'm also not confident that is is good programming form.

My shell programming book doesn't offer me anything that would help me come up with a solution to this specific question. Neither does Google (so far).

This will be used as a solution to this question.

1 Answer 1

2
find "$dir" -type f -printf '%i:%f:%d:%p\n' | 
awk -F: -v OFS=: '{n=$2; gsub(/[^[:alpha:]]/,"",n); $2=length(n); print}'

hmmm, don't need the temp variable:

awk -F: -v OFS=: '{gsub(/[^[:alpha:]]/,"",$2); $2=length($2); print}'
3
  • Thanks! The only issue is I'm getting the alpha count of the entire path instead of the desired basename (filename alone).
    – MountainX
    Mar 29, 2012 at 19:30
  • I think I can live with this. Thank you. I'll accept this answer if a basename answer doesn't come along.
    – MountainX
    Mar 29, 2012 at 20:04
  • @MountainX, updated my answer to only use the basename -- it uses the %f formatting specifier to send the basename to awk where it gets replaced with the "alpha length" Mar 29, 2012 at 20:11

You must log in to answer this question.

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