How to find files, print some standard info about those files, plus print the alpha char count of each filename?

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.

MountainX

Posted 2012-03-29T18:20:48.323

Reputation: 1 735

Answers

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}'

glenn jackman

Posted 2012-03-29T18:20:48.323

Reputation: 18 546

Thanks! The only issue is I'm getting the alpha count of the entire path instead of the desired basename (filename alone). – MountainX – 2012-03-29T19:30:40.273

I think I can live with this. Thank you. I'll accept this answer if a basename answer doesn't come along. – MountainX – 2012-03-29T20:04:33.510

@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" – glenn jackman – 2012-03-29T20:11:49.320