LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 12-12-2015, 02:21 PM   #1
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
BASH - Formating - How to Cap all words but not the 's, 't etc


I have found two ways to cap all words in a string, but it causes it to leave me with words like this,
Code:
 Don'T
, and any other words that have the ' apostrophe in it the letter after it is also capitlized, which is not needed.

Code:
 echo "hi don't think's so" | sed -e  's/\<\(.\)\([A-Za-z]*\)\>/\u\1\L\2/g'
Hi Don'T Think'S So
and
Code:
file="$(echo -e "${file}" | tr "[A-Z]" "[a-z]" | sed -e "s/\b\(.\)/\u\1/g")"
which both do the same thing, leaving the letter just after that apostrophe also upper case which it is not suppose to be. every example loves to show how to upper case everything but not how to handle this situation. gezz -

How do I prevent that from happening?
 
Old 12-12-2015, 02:48 PM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Hi!

Real quick:
Code:
echo "hi don't think's so" | sed 's/\( [a-z]\)/\U\1/g;s/\(^.\)/\U\1/' 
Hi Don't Think's So
There's room for improvement I'm sure, but I'm tired...
 
Old 12-12-2015, 03:07 PM   #3
BW-userx
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

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by HMW View Post
Hi!

Real quick:
Code:
echo "hi don't think's so" | sed 's/\( [a-z]\)/\U\1/g;s/\(^.\)/\U\1/' 
Hi Don't Think's So
There's room for improvement I'm sure, but I'm tired...
I know that feeling -- take a nap -- cheers
 
Old 12-12-2015, 09:31 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am guessing we need the \L because we might have mixed cases elsewhere?
Code:
echo "hi don't think's so" | sed -r 's/([\b ]?)(.)([a-zA-Z]*)/\1\U\2\L\3/g'
 
1 members found this post helpful.
Old 12-12-2015, 10:58 PM   #5
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
Code:
echo "hi don't think's so \"hello\" yoOOUUuu" | perl -pe 's/([\w\x27]+)/\u\L$1/g'
Code:
Hi Don't Think's So "Hello" Yooouuuu

Last edited by Aia; 12-12-2015 at 11:04 PM.
 
1 members found this post helpful.
Old 12-13-2015, 02:42 AM   #6
joel2001k
Member
 
Registered: Mar 2007
Distribution: GNU/Linux debian unstable main
Posts: 95

Rep: Reputation: 17
use read

http://tldp.org/LDP/Bash-Beginners-G...ect_08_02.html
 
Old 12-13-2015, 02:29 PM   #7
BW-userx
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

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
I am guessing we need the \L because we might have mixed cases elsewhere?
Code:
echo "hi don't think's so" | sed -r 's/([\b ]?)(.)([a-zA-Z]*)/\1\U\2\L\3/g'
I was using the other example then it just got to me , I couldn't take it no longer , seeing that letter just after the apostrophe being capped everytime, so I looked up trying to find a way to fix it, the came across that other example. I too got it off the interNet I decided to give it a try becuase that L insures uniformity within the strings, the first char is CAP and the 2 and after is lower case, then it hits that apostrophe then throws everything off, capping the letter just after it. Then my brain got to hurtting trying to figure out how to mask that apostrophe so that it'd see it as an apostrophe and not that other way it sees it- I'm Jargon handycapped
 
Old 12-13-2015, 02:34 PM   #8
BW-userx
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

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Aia View Post
Code:
echo "hi don't think's so \"hello\" yoOOUUuu" | perl -pe 's/([\w\x27]+)/\u\L$1/g'
Code:
Hi Don't Think's So "Hello" Yooouuuu
so what is that code saying in english? where is it telling the BAsh to not see that apostrophe as whatever it sees it as other then part of a sentance? it needs to be told to see the apostrophe as part of the sentense. what part of that code says that, so I'll better unsderstand it. 2. I see you're using escape \ within the text to read, where as what I am doing is dynamic reading file names off a loop (didn't add that info, just showed a command line example, my bag) that'd require more code to have to put excapes within the file name before it is processed, yes?
 
Old 12-13-2015, 04:26 PM   #9
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
Quote:
Originally Posted by BW-userx View Post
so what is that code saying in english?
Code:
perl -pe 's/([\w\x27]+)/\u\L$1/g'

\w : any alphanumeric character
\x27 : single quote in hex, it could be \047 in octal, since the command line would not allow to just place ' without a lot of pain
[\w\x27] : any of these two
[\w\x27]+ : any of these two at least once, but possible more. This will match any word with or without apostrophe.
([\w\x27]+) : capture this group and save it as $1


\u\L$1
$1 : the captured string i.e. word
\L : lower any char until the \E is given, none was given
\u : capitalize the first character

/g : repeat the whole regex as many times as possible.

Last edited by Aia; 12-13-2015 at 04:28 PM.
 
1 members found this post helpful.
Old 12-13-2015, 05:27 PM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Consider using this short test file which is more challenging.
Code:
hi don't think's so
  you've got to be joking!
My country 'tis of thee
'tis the wind and nothing more!
Daniel B. Martin
 
Old 12-13-2015, 05:35 PM   #11
BW-userx
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

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by danielbmartin View Post
Consider using this short test file which is more challenging.
Code:
hi don't think's so
  you've got to be joking!
My country 'tis of thee
'tis the wind and nothing more!
Daniel B. Martin
my copied Internet code
Code:
[userx@voided ~]$ echo "hi don't think's so
>   you've got to be joking!
> My country 'tis of thee
> 'tis the wind and nothing more!" | sed -e  's/\<\(.\)\([A-Za-z]*\)\>/\u\1\L\2/g'
Hi Don'T Think'S So
  You'Ve Got To Be Joking!
My Country 'Tis Of Thee
'Tis The Wind And Nothing More!
@grail code

Code:
[userx@voided ~]$ echo "hi don't think's so
>   you've got to be joking!
> My country 'tis of thee
> 'tis the wind and nothing more!" |  sed -r 's/([\b ]?)(.)([a-zA-Z]*)/\1\U\2\L\3/
g'

Hi Don't Think's So
  you've Got To Be Joking!
My Country 'tis Of Thee
'tis The Wind And Nothing More!
what are we looking 4 here? apples or peaches and cheeries?

Last edited by BW-userx; 12-13-2015 at 05:38 PM.
 
Old 12-13-2015, 05:57 PM   #12
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
If you feel 'tis must be 'Tis, please use the following:

Code:
perl -pe 's/(\w+\x27?(\w+)?)/\u\L$1/g' test.file
Code:
Hi Don't Think's So
  You've Got To Be Joking!
  My Country 'Tis Of Thee
  'Tis The Wind And Nothing More!

  This Line Below Is To Test Single Letters
  A B C'
 
Old 12-13-2015, 07:14 PM   #13
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Aia View Post
If you feel 'tis must be 'Tis, please use the following:

Code:
perl -pe 's/(\w+\x27?(\w+)?)/\u\L$1/g' test.file
Code:
Hi Don't Think's So
  You've Got To Be Joking!
  My Country 'Tis Of Thee
  'Tis The Wind And Nothing More!

  This Line Below Is To Test Single Letters
  A B C'
Nitpick: this result has leading blanks where the InFile did not.

Daniel B. Martin
 
Old 12-13-2015, 07:55 PM   #14
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
Quote:
Originally Posted by danielbmartin View Post
Nitpick: this result has leading blanks where the InFile did not.

Daniel B. Martin
I am afraid you are not nitpick in this instance, but rather leading to irrelevant. Do not get offended.
 
  


Reply

Tags
bash script $@, formatting, strings



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] Formating problem in bash while loop k_balaa Linux - Newbie 9 09-01-2011 09:42 AM
bash alias - two words noir911 Linux - General 4 01-22-2009 09:13 AM
stdout string formating: Bash vs. cpp jhwilliams Linux - Software 1 08-31-2007 05:30 PM
bash script to send email when internet cap is reached aeby Linux - Server 7 08-22-2007 07:46 AM
BASH: First words in a line JordanH Programming 7 10-24-2004 10:00 AM

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

All times are GMT -5. The time now is 09:27 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