# Help with regular expression

**URL:** <https://www.ruby-forum.com/t/help-with-regular-expression/151471>\
**Category:** Ruby\
**Created:** [October 21, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/help-with-regular-expression/151471 "2008-10-21T23:03:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [October 21, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/help-with-regular-expression/151471/1 "2008-10-21T23:03:27Z")

</div>

Team,  
A colleague asked me if I could write a script to:

Read a text file where each record has two words.  
If the second word in the record is not 100% uppercase, write it to a  
file  
and convert it to uppercase.  
I wrote it in Ruby in about 5 lines using  
IO.foreach("/tmp/somefile.txt") do  
|file| for the input file and fo = File.open("/tmp/somefile.out",“a+”) for  
the output file, upcase method.  
Then she threw a curve ball at me telling me that she wanted it in Korn  
Shell using regular expression.  
The facts are that I don’t know how to do this using regular  
expressions.  
I am not asking anyone to solve for me, but if you can tell me:

How do you compare, using regular expression, the second word in the  
input  
record for upper case. In other words, if the second word has at least 1  
lower-case char, it has to be flagged and translated to upper-case.

I will deal with the I/O issues in Korn Shell.

Thank you

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [October 21, 2008, 11:07pm UTC](https://www.ruby-forum.com/t/help-with-regular-expression/151471/2 "2008-10-21T23:07:05Z")

</div>

> How do you compare, using regular expression, the second word in the  
> input  
> record for upper case.

man sed  
man expr  
man tr

For further help, try the comp.unix.shell group

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [October 21, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/help-with-regular-expression/151471/3 "2008-10-21T23:14:02Z")

</div>

/\b[A-Z]+$/  
will match if last word in string is not 100% upppercase

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [October 21, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/help-with-regular-expression/151471/4 "2008-10-21T23:14:37Z")

</div>

I think you want to use

string =~ ere

ere is an extended regular expression.

You might also be able to exploit:

typeset -u somevar

which causes $somevar to be upcased  
(e.g., somevar=‘hello’; [$somevar == ‘HELLO’] is true)

man ksh is now your friend.

-Rob

On Oct 21, 2008, at 2:17 PM, Evgeniy D. wrote:

> > ## a file expressions. Thank you
> > 
> > Ruby S.

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [October 21, 2008, 11:25pm UTC](https://www.ruby-forum.com/t/help-with-regular-expression/151471/5 "2008-10-21T23:25:39Z")

</div>

Also:

while read firstword rest; do  
echo “First word is $firstword”  
echo “Rest of line is $rest”  
done

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [October 22, 2008, 12:17am UTC](https://www.ruby-forum.com/t/help-with-regular-expression/151471/6 "2008-10-22T00:17:30Z")

</div>

On Wed, 22 Oct 2008, Evgeniy D. wrote:

> /\b[A-Z]+$/  
> will match if last word in string is not 100% upppercase  
> s/not//

If you convert a string to uppercase, and it already is all uppercase,  
then it will be unchanged… I’d use awk if a dependence on ruby is  
not allowed, and no regexp needed.

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [October 22, 2008, 1:45am UTC](https://www.ruby-forum.com/t/help-with-regular-expression/151471/7 "2008-10-22T01:45:16Z")

</div>

On Tue, Oct 21, 2008 at 2:29 PM, Hugh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> not allowed, and no regexp needed.
> 
> > > I wrote it in Ruby in about 5 lines using  
> > > How do you compare, using regular expression, the second word in the

Thanks to everyone for your help!

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [October 22, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/help-with-regular-expression/151471/8 "2008-10-22T17:04:15Z")

</div>

On Tue, Oct 21, 2008 at 5:24 PM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Also:
> 
> ## while read firstword rest; do echo “First word is $firstword” echo “Rest of line is $rest” done
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Thanks to everyone for your help and recommendations!
