Register
It is currently Fri Jul 25, 2014 12:16 pm

sed - convert Upper to Title Case


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Fri Sep 11, 2009 2:53 pm   

Joined: Thu Sep 10, 2009 7:00 pm
Posts: 1
Hello!

I am new to using regex expressions, and am having difficulty with the syntax creating workable regexs.

I hope this not too much for one post. But I have been working on this for a while and can't get beyond the basics. Any suggestions would be appreciated.

I am using BSD SED on a Mac 10.3.9

My first project:

Source Text:

D08/14/2009
NN/A
PBAJA FISH TACOS
T-8.45
^
I want to convert the text after the P - BAJA FISH TACOS - to title case.

I have focused on Sed as my tool of choice, but wonder if Perl may be a better choice.

I want to write a script to covert selected pieces of text in a file from all CAPS to Title Case. Ultimately, will want to use lookbehind to capture text after delimitating P. Which would I believe the regex would be:
(?<=^P).*

I can't seem to get started with SED. Before I show you the potential solutions, I would like to ask some basic questions to enhance my regex knowledge.

To build my knowledge of regex use I tried a few basic regex building blocks.

Want to do a simple replace, but only if text is at beginning of word.

LEARN SED TEST #1:
_testsed_ source text:
In Xinanadu did Kinubla Khan
A stately in pleasure dome decree:
Where Alph, inthe sacred river, ran
Through caveinrns meinasureless to man

Working from the command line:
sed 's/\bin/xx/g' testsed

I also tried unsuccessfully the \< metasequences (in case there was an issue with \b metacharacter)
sed 's/\<in\>/xx/g' testsed

No changes, seems to not find anything.

Not finding anything seems odd. I tried \bin in several text editors that support regex and it both (in line 2 and line 4). So maybe something is wrong with bash syntax?

If I Remove the \b boundary and xx replaces all instances of in with xx.

I also tried these variations to help determine my mistake:
sed 's/\bin\b/xx/g' testsed
sed 's/\bin/xx/g' testsed
sed 's/\b(in)/xx/g' testsed
sed 's/\b([in])/xx/g' testsed

I want to start with these "building block" before moving on to my difficult task.

Any suggestions on why the above doesn't work would be greatly appreciated. I am going to add my follow on question on potential solutions to my problem, but want to understand the simple before diving in to the deep end, so to speak.


CONVERT CASE PROJECT
On to the problem at hand. (I am eliminating the lookbehind to simplify.)
I found these on the bashscripts.org board & a link from a post discussing these.

Potential Solution #1:
Sample Text:

THE SOMEWHERE STORE
LOCAL FOODS
A GOOD PLACE TO BE

Title Case
Code:
sed 's/\(\<.\)\.*/\u\1/g' filename;

This is how I break down the 1st sed:

First () group - hold for backreference - $1
\<.\> first character of each word
\.* rest of word
\u\1 convert first backreference -$1- to lower case.

Am I reading this correctly?

First can someone explain what I am doing wrong in Test #1?

Second, if not too much trouble, can someone guide me through my CONVERT CASE PROJECT? Would like to know; Is sed the proper tool? Does \u work as my SED reference indicates? If so, some feed back on my solution and explanation on my regex.

Again, I know this is a lot, but I have been working on this for a bit, and can't get beyond these basics, particularly in my basic effort Test #1.

Thank you,
dmp


Top
 Profile  
 PostPosted: Fri Sep 11, 2009 4:37 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
Something like this ?
Code:
echo "THE SOMEWHERE STORE
LOCAL FOODS
A GOOD PLACE TO BE" | sed 's/.*/\L&/; s/[a-z]*/\u&/g'
The Somewhere Store
Local Foods
A Good Place To Be


Top
 Profile  
 PostPosted: Wed Nov 24, 2010 11:22 am   
User avatar

Joined: Wed Nov 17, 2010 1:02 pm
Posts: 9
Location: North Bethesda, MD
Watael wrote:
Something like this ?
Code:
echo "THE SOMEWHERE STORE
LOCAL FOODS
A GOOD PLACE TO BE" | sed 's/.*/\L&/; s/[a-z]*/\u&/g'
The Somewhere Store
Local Foods
A Good Place To Be


Can you break down that sed statement please?


Top
 Profile WWW  
 PostPosted: Fri Nov 26, 2010 3:23 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
\L is lower-case all, \u is upper-case first character, & is the whole matching string.


Top
 Profile  
 PostPosted: Sat Jul 02, 2011 11:00 am   
User avatar

Joined: Sat Jul 02, 2011 10:43 am
Posts: 23
Location: Devon,UK
Watael wrote:
Something like this ?
Code:
echo "THE SOMEWHERE STORE
LOCAL FOODS
A GOOD PLACE TO BE" | sed 's/.*/\L&/; s/[a-z]*/\u&/g'
The Somewhere Store
Local Foods
A Good Place To Be


Was just looking for this solution thanks!
Never seen this site before but I do a fair amount of bash scripting so I've just signed up. :)


Top
 Profile WWW  
 PostPosted: Sat Jul 02, 2011 11:13 am   
User avatar

Joined: Sat Jul 02, 2011 10:43 am
Posts: 23
Location: Devon,UK
P.S

the code should be
Code:
sed 's/.*/\L&/; s/[[:graph:]]*/\u&/g'

otherwise apostrophes muck it up CAN'T becomes Can'T etc


Top
 Profile WWW  
 PostPosted: Sat Oct 08, 2011 9:51 am   

Joined: Sat Oct 08, 2011 9:45 am
Posts: 1
Great work!
This is the first time I've seen a bash script take care of the single quotes. I've had to write a c++ snippet to place in my bin directory to do this!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP