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 |
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.
|
 |
06-30-2017, 12:13 PM
|
#1
|
Member
Registered: Sep 2016
Location: Webster MA USA
Posts: 243
Rep: 
|
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
|
|
|
06-30-2017, 12:28 PM
|
#2
|
LQ Veteran
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky 9.6
Posts: 5,906
|
Quote:
Originally Posted by L_Carver
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?
|
|
|
06-30-2017, 12:30 PM
|
#3
|
LQ Guru
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
|
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'
|
|
|
06-30-2017, 01:03 PM
|
#4
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,968
|
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.
|
|
|
06-30-2017, 01:43 PM
|
#5
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
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
|
|
|
06-30-2017, 07:40 PM
|
#6
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,441
|
I use a variant of @hydrurga's offering, but using word bounding. Never seen the need to convert to lowercase first.
|
|
|
06-30-2017, 07:50 PM
|
#7
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
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.
|
|
|
06-30-2017, 09:40 PM
|
#8
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,441
|
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.
|
|
|
All times are GMT -5. The time now is 02:43 PM.
|
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
|
|