LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-18-2006, 11:01 AM   #1
Yalla-One
Member
 
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 641

Rep: Reputation: 36
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
 
Old 04-18-2006, 12:06 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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]'
}
 
Old 04-18-2006, 12:12 PM   #3
Yalla-One
Member
 
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 641

Original Poster
Rep: Reputation: 36
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
 
Old 04-18-2006, 01:01 PM   #4
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
Cool

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.
Old 04-19-2006, 09:21 AM   #5
Yalla-One
Member
 
Registered: Oct 2004
Location: Norway
Distribution: Slackware, CentOS
Posts: 641

Original Poster
Rep: Reputation: 36
Works like a charm - thanks much puffinman!
 
Old 11-26-2009, 12:08 AM   #6
justanothertechie
LQ Newbie
 
Registered: Nov 2009
Posts: 1

Rep: Reputation: 0
great solution

Thanks HKO.. that worked just great.

I used it to rename some music files nad it did the job perfectly
 
Old 01-29-2010, 03:48 PM   #7
chasepeeler
LQ Newbie
 
Registered: Jan 2010
Posts: 2

Rep: Reputation: 0
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
>
 
Old 02-01-2010, 02:28 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by chasepeeler View Post
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.
 
Old 02-02-2010, 11:56 AM   #9
chasepeeler
LQ Newbie
 
Registered: Jan 2010
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by konsolebox View Post
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,,}^}
 
Old 02-03-2010, 02:37 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by chasepeeler View Post
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.
 
  


Reply

Tags
perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script: Delete filenames containing a substring? Drack Linux - General 4 02-12-2006 03:11 PM
How do I use an If statement within a case in a Shell script?? crazygyhrous Programming 7 01-03-2006 06:41 AM
help changing case on arguments to bourne shell script Maldain Programming 2 05-03-2005 10:18 AM
converting shell script to php script ? ibro Linux - General 6 05-24-2004 05:19 AM
bash shell script appends '?' to filenames sceptreofjudah Linux - Newbie 3 03-18-2004 08:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration