LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-30-2017, 12:13 PM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 241

Rep: Reputation: Disabled
Question Change case of string to "Title case" (Kate name for it).


I'm looking for a way to change the case of every first letter of a string with more than one word in it, such as
parlour car
to
Parlour Car.

I prefer, for my own reasons (a clue to one of which is in two lines of my signature), to do so on the command line rather than in\with Kate or Notepadqq.

Is there a simple way, in bash or perl, to accomplish this?

Carver
 
Old 06-30-2017, 12:28 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 5,435

Rep: Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066
Quote:
Originally Posted by L_Carver View Post
I'm looking for a way to change the case of every first letter of a string with more than one word in it, such as
parlour car
to
Parlour Car.

I prefer, for my own reasons (a clue to one of which is in two lines of my signature), to do so on the command line rather than in\with Kate or Notepadqq.

Is there a simple way, in bash or perl, to accomplish this?

Carver
Yes there is. What have you tried?
 
Old 06-30-2017, 12:30 PM   #3
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 20 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918Reputation: 2918
Entering bash "title case" in Google Search produces the following as the first result:

http://www.commandlinefu.com/command...-to-title-case

which suggests:

Code:
echo "this is a test" | sed 's/.*/\L&/; s/[a-z]*/\u&/g'
 
Old 06-30-2017, 01:03 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,354
Blog Entries: 13

Rep: Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411Reputation: 4411
There is at least one Linux shell command which can change case, and there are a number of commands which can replace characters, thereby also changing case. Have look at the tr command, however I believe awk and sed can obviously edit files as well. I also feel it depends whether you wish to do this using Perl or a bash script. I'm guessing you can call Linux commands using Perl much like you can do this using a bash script.

It would be helpful to know if you have yet tried anything, or at the very least, to know whether you're more comfortable with bash or Perl.
 
Old 06-30-2017, 01:43 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Code:
userx%slackwhere ⚡ imaging ⚡> foo="gg boo"
userx%slackwhere ⚡ imaging ⚡> doo=${foo^}
userx%slackwhere ⚡ imaging ⚡> echo $doo
Gg boo

userx%slackwhere ⚡ imaging ⚡> echo "$(echo $foo | sed -e "s/\b\(.\)/\u\1/g")"
Gg Boo
 
Old 06-30-2017, 07:40 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,773

Rep: Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572
I use a variant of @hydrurga's offering, but using word bounding. Never seen the need to convert to lowercase first.
 
Old 06-30-2017, 07:50 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
I believe that dropping it to lower case first ensures proper results.
Code:
userx%slackwhere ⚡ ~ ⚡> foo="DO FO FO FO ER TERTEE"                             
userx%slackwhere ⚡ ~ ⚡> echo  "$(echo $foo | sed -e "s/\b\(.\)/\u\1/g")"
DO FO FO FO ER TERTEE
userx%slackwhere ⚡ ~ ⚡> foo="Do be de fo ra"                                    
userx%slackwhere ⚡ ~ ⚡> echo  "$(echo $foo | sed -e "s/\b\(.\)/\u\1/g")"
Do Be De Fo Ra


userx%slackwhere ⚡ ~ ⚡> foo="DO FO FO FO ER TERTEE"                             
userx%slackwhere ⚡ ~ ⚡> echo $foo | sed 's/.*/\L&/; s/[a-z]*/\u&/g'
Do Fo Fo Fo Er Tertee

Last edited by BW-userx; 06-30-2017 at 07:51 PM.
 
Old 06-30-2017, 09:40 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,773

Rep: Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572Reputation: 3572
The OP only wanted to change the first letter - I learnt a long ime ago not to munge the users data beyond what they asked for.
Whether it was sensible was a cause perhaps for a query, but I did what I was asked for. That way other arses got kicked, not mine.
 
  


Reply


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
[SOLVED] Bash: Checking for lower case and upper case letters in a string fatalerror0x00 Programming 1 12-09-2012 02:17 AM
Help with sed and awk to change L-case letters to U-case for specific lines in a file rootaccess Linux - General 12 05-21-2012 02:50 PM
How to change Case of a string(BASH Scripting)? pinga123 Linux - Newbie 4 07-09-2010 12:31 AM
output the path for files whose names include string "string" (case insensitive) sean_zhang Linux - Newbie 1 03-04-2008 11:59 PM
change case of a string (TOUPPER) in shell script raees Linux - Software 3 05-03-2005 02:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:33 PM.

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