[mdlug] How to find file name length?

Paul set at pobox.com
Fri Sep 28 01:43:45 EDT 2007


Raymond McLaughlin <driveray at ameritech.net>, on Thu Sep 27, 2007 [02:20:29 AM] said:
> Paul wrote:
> > Raymond McLaughlin <driveray at ameritech.net>, on Wed Sep 26, 2007 [06:51:55 PM] said:
> >> Also did any one notice that the "echo "$(basename "{}" | wc -c" part
> >> doesn't work as intended? Observe:
> >>
> >>     echo filename.txt | wc -c
> >>     13
> >>
> >> wc counts the trailing new line as a character, you need to use "echo -n"
> >>
> >>     echo -n filename.txt | wc -c
> >>     12
> >>
> >>
> >> Ray
> > 
> > 	Hi;
> > 
> > 	Yeah, I noticed;) its called slop. You cant just shove a
> > -n into my example without having to do something extra, since Im
> > just using one echo, the -n wouldn't truncate the right newline,
> > and you would just get a big blob of output. (the result length
> > is calculated before echo gets involved.)
> 
> You are right, you can't just shove a -n  into your example, but "echo
> -n" still works, if you  shove in a whole "echo -n $(...)"
> 
> Your example:
> 
> find -type f -exec bash -c 'echo "$(basename "{}" | wc -c) {}" '
> 
> adding echo -n makes it work, at least in my limited testing:
> 
> find -type f -exec bash -c 'echo "$(echo -n $(basename "{}") | wc -c) {}" '
> 
> Ray

	Hi;

	Well, I cant argue with making something quick dirty and
ugly even more ugly and slow;) Pointing out that including the
newline offs the count by one was a helpful educational
observation, though. I meerly meant to say that I wasnt crafting
anything special and that was a concious choice...
	And of course MD found a good option to find to make everything
much slicker.
	Actually, what I really wonder is why 'use UDF' wasnt the
right answer? :) But I know people love these scripting threads....

Paul
set at pobox.com



More information about the mdlug mailing list