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

Forum statistics

Threads
473,818
Messages
2,569,732
Members
45,691
Latest member
Dick331194

Latest Threads

Top