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:
PBAJA FISH TACOS
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:
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:
THE SOMEWHERE STORE
A GOOD PLACE TO BE
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.