Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-18-2006, 11:01 AM
|
#1
|
Member
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 641
Rep:
|
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
|
|
|
04-18-2006, 12:06 PM
|
#2
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
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]'
}
|
|
|
04-18-2006, 12:12 PM
|
#3
|
Member
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 641
Original Poster
Rep:
|
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
|
|
|
04-18-2006, 01:01 PM
|
#4
|
Member
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217
Rep:
|
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: $!";
}
|
|
1 members found this post helpful.
|
04-19-2006, 09:21 AM
|
#5
|
Member
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 641
Original Poster
Rep:
|
Works like a charm - thanks much puffinman!
|
|
|
11-26-2009, 12:08 AM
|
#6
|
LQ Newbie
Registered: Nov 2009
Posts: 1
Rep:
|
great solution
Thanks HKO.. that worked just great.
I used it to rename some music files nad it did the job perfectly
|
|
|
01-29-2010, 03:48 PM
|
#7
|
LQ Newbie
Registered: Jan 2010
Posts: 2
Rep:
|
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
>
|
|
|
02-01-2010, 02:28 AM
|
#8
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
Quote:
Originally Posted by chasepeeler
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.
|
|
|
02-02-2010, 11:56 AM
|
#9
|
LQ Newbie
Registered: Jan 2010
Posts: 2
Rep:
|
Quote:
Originally Posted by konsolebox
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
|
|
|
02-03-2010, 02:37 AM
|
#10
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
Quote:
Originally Posted by chasepeeler
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
|
Guess we can't do that in bash. Maybe in zsh.
|
|
|
All times are GMT -5. The time now is 03:46 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|