Help with regular expression

Discussion in 'Ruby' started by Ruby Student, Oct 21, 2008.

  1. Ruby Student

    Ruby Student Guest

    [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
    --
    Ruby Student
     
    Ruby Student, Oct 21, 2008
    #1
    1. Advertising

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

    On Tue, Oct 21, 2008 at 10:08 PM, Ruby Student <> wrote:
    > 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
    > --
    > Ruby Student
    >
     
    Evgeniy Dolzhenko, Oct 21, 2008
    #2
    1. Advertising

  3. 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:

    > /\b[A-Z]+$/
    > will match if last word in string is not 100% upppercase
    >
    > On Tue, Oct 21, 2008 at 10:08 PM, Ruby Student
    > <> wrote:
    >> 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
    >> --
    >> Ruby Student
    >>

    >


    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Oct 21, 2008
    #3
  4. Ruby Student

    Hugh Sasse Guest

    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.
    >
    > On Tue, Oct 21, 2008 at 10:08 PM, Ruby Student <> wrote:
    > > 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
    > > --
    > > Ruby Student
    > >

    >
     
    Hugh Sasse, Oct 21, 2008
    #4
  5. Ruby Student

    Ruby Student Guest

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

    On Tue, Oct 21, 2008 at 2:29 PM, Hugh Sasse <> wrote:

    >
    >
    > 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.
    > >
    > > On Tue, Oct 21, 2008 at 10:08 PM, Ruby Student <>

    > wrote:
    > > > 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
    > > > --
    > > > Ruby Student
    > > >

    > >

    >
    >

    Thanks to everyone for your help!

    --
    Ruby Student
     
    Ruby Student, Oct 21, 2008
    #5
  6. > 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Oct 21, 2008
    #6
  7. 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/.
     
    Brian Candler, Oct 21, 2008
    #7
  8. Ruby Student

    Ruby Student Guest

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

    On Tue, Oct 21, 2008 at 5:24 PM, Brian Candler <> 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!

    --
    Ruby Student
     
    Ruby Student, Oct 22, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anand

    Regular Expression help...

    Anand, Jul 9, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,246
    Eric J. Roode
    Jul 9, 2003
  2. Eric B.
    Replies:
    1
    Views:
    440
    Jim Gibson
    Dec 17, 2004
  3. VSK
    Replies:
    2
    Views:
    2,388
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    884
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    12,049
    vforvikash
    Dec 29, 2008
Loading...

Share This Page