Help with regular expression

R

Ruby Student

[Note: parts of this message were removed to make it a legal post.]

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
 
R

Rob Biedenharn

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

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

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

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
H

Hugh Sasse

/\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.
 
R

Ruby Student

[Note: parts of this message were removed to make it a legal post.]

/\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.
Thanks to everyone for your help!
 
B

Brian Candler

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
 
B

Brian Candler

Also:

while read firstword rest; do
echo "First word is $firstword"
echo "Rest of line is $rest"
done
 
R

Ruby Student

[Note: parts of this message were removed to make it a legal post.]

Also:

while read firstword rest; do
echo "First word is $firstword"
echo "Rest of line is $rest"
done
Thanks to everyone for your help and recommendations!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,829
Messages
2,569,737
Members
45,525
Latest member
RosalindSa

Latest Threads

Top