how to delete a line in a text file

K

khaihkd

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
 
T

Thibault Jouan

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

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]
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top