[SOLVED] BASH - Formating - How to Cap all words but not the 's, 't etc
ProgrammingThis 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.
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.
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 -
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
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?
\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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.