Forum: Ruby Help with regular expression

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
64a808c19ac542b14c4021dcb9484a37?d=identicon&s=25 Ruby Student (Guest)
on 2008-10-21 23:03
(Received via mailing list)
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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-10-21 23:07
> 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
1700eeb3b7d7cc960e03a016be7dcaff?d=identicon&s=25 Evgeniy Dolzhenko (Guest)
on 2008-10-21 23:14
(Received via mailing list)
/\b[A-Z]+$/
will match if last word in string is not 100% upppercase
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-10-21 23:14
(Received via mailing list)
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 Dolzhenko wrote:

>> a file
>> expressions.
>> Thank you
>> --
>> Ruby Student
>>
>

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-10-21 23:25
Also:

while read firstword rest; do
  echo "First word is $firstword"
  echo "Rest of line is $rest"
done
457cf540784a12ba2f30e06565a2c189?d=identicon&s=25 Hugh Sasse (Guest)
on 2008-10-22 00:17
(Received via mailing list)
On Wed, 22 Oct 2008, Evgeniy Dolzhenko 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.
64a808c19ac542b14c4021dcb9484a37?d=identicon&s=25 Ruby Student (Guest)
on 2008-10-22 01:45
(Received via mailing list)
On Tue, Oct 21, 2008 at 2:29 PM, Hugh Sasse <hgs@dmu.ac.uk> 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!
64a808c19ac542b14c4021dcb9484a37?d=identicon&s=25 Ruby Student (Guest)
on 2008-10-22 17:04
(Received via mailing list)
On Tue, Oct 21, 2008 at 5:24 PM, Brian Candler <b.candler@pobox.com>
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/.
>
>
Thanks to everyone for your help and recommendations!
This topic is locked and can not be replied to.