Deleting Text From a File

Discussion in 'Ruby' started by woodyee, Nov 28, 2007.

  1. woodyee

    woodyee Guest

    Hi! I had to manually remove decimals and tildes from a text file. How
    could I have done this via programming? I googled this but didn't
    really find anything, just stuff on removing spaces. Basically, I'd
    like to be able to search a file and remove certain items (ex - all
    decimals and tildes). Thanks!
    woodyee, Nov 28, 2007
    #1
    1. Advertising

  2. woodyee

    Alex Young Guest

    woodyee wrote:
    > Hi! I had to manually remove decimals and tildes from a text file. How
    > could I have done this via programming? I googled this but didn't
    > really find anything, just stuff on removing spaces. Basically, I'd
    > like to be able to search a file and remove certain items (ex - all
    > decimals and tildes). Thanks!
    >

    Sounds like a job for regular expressions. Have you got a short before
    and after example?

    --
    Alex
    Alex Young, Nov 28, 2007
    #2
    1. Advertising

  3. woodyee

    woodyee Guest

    On Nov 28, 9:44 am, Alex Young <> wrote:
    > woodyee wrote:
    > > Hi! I had to manually remove decimals and tildes from a text file. How
    > > could I have done this via programming? I googled this but didn't
    > > really find anything, just stuff on removing spaces. Basically, I'd
    > > like to be able to search a file and remove certain items (ex - all
    > > decimals and tildes). Thanks!

    >
    > Sounds like a job for regular expressions. Have you got a short before
    > and after example?
    >
    > --
    > Alex


    Sure. See below. Basically, the entire document was like this. To me,
    Reg Exp's were the key but they're a weakness of mine so I was
    lost. :)


    Original
    ---------
    89.00~~~~
    6330.00~15~CAT1~0005~1.00

    Modified
    ----------
    89
    6330 15 CAT1 0005 1
    woodyee, Nov 28, 2007
    #3
  4. woodyee

    Alex Young Guest

    woodyee wrote:
    > On Nov 28, 9:44 am, Alex Young <> wrote:
    >> woodyee wrote:
    >>> Hi! I had to manually remove decimals and tildes from a text file. How
    >>> could I have done this via programming? I googled this but didn't
    >>> really find anything, just stuff on removing spaces. Basically, I'd
    >>> like to be able to search a file and remove certain items (ex - all
    >>> decimals and tildes). Thanks!

    >> Sounds like a job for regular expressions. Have you got a short before
    >> and after example?
    >>
    >> --
    >> Alex

    >
    > Sure. See below. Basically, the entire document was like this. To me,
    > Reg Exp's were the key but they're a weakness of mine so I was
    > lost. :)
    >
    >
    > Original
    > ---------
    > 89.00~~~~
    > 6330.00~15~CAT1~0005~1.00
    >
    > Modified
    > ----------
    > 89
    > 6330 15 CAT1 0005 1
    >


    irb(main):001:0> file = "89.00~~~~
    irb(main):002:0" 6330.00~15~CAT1~0005~1.00
    irb(main):003:0"
    irb(main):004:0" "
    => "89.00~~~~\n6330.00~15~CAT1~0005~1.00\n\n"
    irb(main):005:0> file.gsub(/\.\d+/, '').tr('~', ' ')
    => "89 \n6330 15 CAT1 0005 1\n\n"
    irb(main):006:0> puts _
    89
    6330 15 CAT1 0005 1

    I think that should do it :) I don't think it's feasible to do it all
    in a single pass, which is why I've gone for separate gsub and tr phases.

    --
    Alex
    Alex Young, Nov 28, 2007
    #4
  5. woodyee

    MonkeeSage Guest

    On Nov 28, 8:38 am, woodyee <> wrote:
    > Hi! I had to manually remove decimals and tildes from a text file. How
    > could I have done this via programming? I googled this but didn't
    > really find anything, just stuff on removing spaces. Basically, I'd
    > like to be able to search a file and remove certain items (ex - all
    > decimals and tildes). Thanks!


    How about...

    "he~~ll.o".tr('~.', '') # => "hello"

    String#tr translates a group of characters into another group, such
    that for every character in a given position in group one, it is
    replaced by the character in the same position in group two, or if
    group two is shorter, it uses the last character in group two. In this
    case, group two is empty, so everything in group one is replaced with
    empty strings (i.e., deleted).

    HTH,
    Jordan
    MonkeeSage, Nov 28, 2007
    #5
  6. woodyee

    yermej Guest

    On Nov 28, 8:38 am, woodyee <> wrote:
    > Hi! I had to manually remove decimals and tildes from a text file. How
    > could I have done this via programming? I googled this but didn't
    > really find anything, just stuff on removing spaces. Basically, I'd
    > like to be able to search a file and remove certain items (ex - all
    > decimals and tildes). Thanks!


    This will remove all decimals and tildes and create a backup of the
    original file with the extension .bak (from the command line):

    ruby -i.bak -n -e 'print $_.gsub(/[\.~]/, "")' input_file.txt

    Jeremy
    yermej, Nov 28, 2007
    #6
  7. woodyee

    woodyee Guest

    On Nov 28, 2:00 pm, yermej <> wrote:
    > On Nov 28, 8:38 am, woodyee <> wrote:
    >
    > > Hi! I had to manually remove decimals and tildes from a text file. How
    > > could I have done this via programming? I googled this but didn't
    > > really find anything, just stuff on removing spaces. Basically, I'd
    > > like to be able to search a file and remove certain items (ex - all
    > > decimals and tildes). Thanks!

    >
    > This will remove all decimals and tildes and create a backup of the
    > original file with the extension .bak (from the command line):
    >
    > ruby -i.bak -n -e 'print $_.gsub(/[\.~]/, "")' input_file.txt
    >
    > Jeremy



    WOW! It worked and it was so cool!! Thanks! One thing I did - I
    modified it to delete the .00's:

    ruby -i.bak -n -e 'print $_.gsub(/[\.00~]/, " ")' input_file.txt

    This worked but it deleted the zero in front of the decimal. How can I
    avoid this? Thanks! :)
    woodyee, Nov 28, 2007
    #7
  8. woodyee

    yermej Guest

    On Nov 28, 3:50 pm, woodyee <> wrote:
    > On Nov 28, 2:00 pm, yermej <> wrote:
    >
    > > On Nov 28, 8:38 am, woodyee <> wrote:

    >
    > > > Hi! I had to manually remove decimals and tildes from a text file. How
    > > > could I have done this via programming? I googled this but didn't
    > > > really find anything, just stuff on removing spaces. Basically, I'd
    > > > like to be able to search a file and remove certain items (ex - all
    > > > decimals and tildes). Thanks!

    >
    > > This will remove all decimals and tildes and create a backup of the
    > > original file with the extension .bak (from the command line):

    >
    > > ruby -i.bak -n -e 'print $_.gsub(/[\.~]/, "")' input_file.txt

    >
    > > Jeremy

    >
    > WOW! It worked and it was so cool!! Thanks! One thing I did - I
    > modified it to delete the .00's:
    >
    > ruby -i.bak -n -e 'print $_.gsub(/[\.00~]/, " ")' input_file.txt
    >
    > This worked but it deleted the zero in front of the decimal. How can I
    > avoid this? Thanks! :)


    Typically, when you characters in [], it means to match any character
    in that group. So, [\.00~] is the same as [\.0~] is the same as [0\.~]
    etc. so you removed all occurrences of 0, ., and ~ no matter what
    surrounded them.

    If you want to delete just .00 and ~, try:

    /\.00|~/

    as the first argument to gsub. That means match .00 or ~ and nothing
    else.

    This is probably a decent site if you want to learn more about regular
    expressions:
    http://www.regular-expressions.info/tutorial.html

    Jeremy
    yermej, Nov 28, 2007
    #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. GMK

    Deleting Content of a text file

    GMK, Mar 4, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    376
  2. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,391
    Scott Allen
    Jun 4, 2005
  3. Harry Barker
    Replies:
    2
    Views:
    504
    Alf P. Steinbach
    Apr 19, 2006
  4. Joey Martin

    Deleting blank lines from text file

    Joey Martin, Aug 30, 2005, in forum: ASP General
    Replies:
    1
    Views:
    190
    Evertjan.
    Aug 30, 2005
  5. crea
    Replies:
    2
    Views:
    397
    Nobody
    Dec 28, 2012
Loading...

Share This Page