how to delete a line in a text file

Discussion in 'Ruby' started by khaihkd, Feb 17, 2014.

  1. khaihkd

    khaihkd Guest

    my file is text.txt with content

    resources :albums, except: [:new, :edit, :destroy]
    resources :conversation_replies, except: [:new, :edit, :destroy]
    resources :authors, except: [:new, :edit, :destroy]
    .........

    Now, I want to delete a line in this file. example, I want delete line with content with the characters such as: conversation_replies

    Please help me. thank you very much
     
    khaihkd, Feb 17, 2014
    #1
    1. Advertisements

  2. You may look at String#include, and depending on your use case,
    maybe File#open and/or Tempfile#new.

    Simple ruby example program that come to mind:


    $ cat filter.rb
    #!/usr/bin/env ruby

    pattern = ARGV.shift

    ARGF.each_line { |e| puts e unless e.include? pattern }

    $ cat text.txt
    resources :albums, except: [:new, :edit, :destroy]
    resources :conversation_replies, except: [:new, :edit, :destroy]
    resources :authors, except: [:new, :edit, :destroy]

    $ ./filter.rb conversation_replies text.txt
    resources :albums, except: [:new, :edit, :destroy]
    resources :authors, except: [:new, :edit, :destroy]
     
    Thibault Jouan, Feb 18, 2014
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.