LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script for converting filenames to proper case? (https://www.linuxquestions.org/questions/programming-9/shell-script-for-converting-filenames-to-proper-case-436406/)

Yalla-One 04-18-2006 11:01 AM

shell script for converting filenames to proper case?
 
Hi,

Does anyone have a shell script to recommend to convert all files in a directory to proper case? (if the character previous to this is space, then capitalize, if not, set lower-case).

I assume there's a clever way to do this with bash+sed+awk, but with limited knowledge I do not even know where to start googling...

Thanks for any pointers or ideas... The ideal thing would be a bash function such as "convert_to_upper()" that took care of it ;)

-Y1

Hko 04-18-2006 12:06 PM

Quote:

Originally Posted by Yalla-One
The ideal thing would be a bash function such as "convert_to_upper()" that took care of it ;)

Code:

#!/bin/bash

convert_to_upper() {
    echo "$1" | tr '[a-z]' '[A-Z]'
}


Yalla-One 04-18-2006 12:12 PM

Hi and thanks for answering.

Good start, but this only converts lower to upper, not miXeD cAsE to Proper Case (first after space=CAPS - rest small)

Anyone?

-Y1

puffinman 04-18-2006 01:01 PM

Code:

#!/usr/bin/perl
# nicecase - proper case on icky filenames with spaces
foreach (@ARGV) {
  $was = $_;
  $_ = join ' ', map {ucfirst(lc($_))} split /\s+/, $_;
  rename($was,$_) or warn "Couldn't rename $was: $!";
}


Yalla-One 04-19-2006 09:21 AM

Works like a charm - thanks much puffinman!

justanothertechie 11-26-2009 12:08 AM

great solution
 
Thanks HKO.. that worked just great.

I used it to rename some music files nad it did the job perfectly

chasepeeler 01-29-2010 03:48 PM

bash script
 
Here is a pretty simple bash script


Code:

#!/bin/bash

for i in $@; do
  i=${i,,}
  echo -n "${i^} "
done
echo

Code:

> ./propercase abcdef ABCDEF aBcDeF AbCdEf abcDEF ABCdef
Abcdef Abcdef Abcdef Abcdef Abcdef Abcdef
>


konsolebox 02-01-2010 02:28 AM

Quote:

Originally Posted by chasepeeler (Post 3845389)
Here is a pretty simple bash script
Code:

#!/bin/bash

for i in $@; do
  i=${i,,}
  echo -n "${i^} "
done
echo


You can also omit 'in $@' there. Nice code for bash 4.0.

chasepeeler 02-02-2010 11:56 AM

Quote:

Originally Posted by konsolebox (Post 3847986)
You can also omit 'in $@' there. Nice code for bash 4.0.

Usually I am all for shortcuts, but I've found in instances like this, the second saved from omitting the 'in $@' usually leads to headaches down the road if the script is modified or used as a basis for a larger script.

What I do wish is there was a way to combine the two string functions into one, like
Code:

echo -n ${{i,,}^}

konsolebox 02-03-2010 02:37 AM

Quote:

Originally Posted by chasepeeler (Post 3849792)
Usually I am all for shortcuts, but I've found in instances like this, the second saved from omitting the 'in $@' usually leads to headaches down the road if the script is modified or used as a basis for a larger script.

That doesn't really apply on my large scripts but well, that's an option.
Quote:

What I do wish is there was a way to combine the two string functions into one, like
Code:

echo -n ${{i,,}^}

Guess we can't do that in bash. Maybe in zsh.


All times are GMT -5. The time now is 08:31 PM.