简体   繁体   中英

How to delete a specific line of a text file?

So, I have been browsing tall and high for a solution, but came up short. So, here is my problem:

how do I write a program, that will delete a specific line from a text file?

The information is as follows:

Lukas;11111111;x6 y8
John;22222222;x7 y10
Fred;44444444;x8 y15

The info is basically name, phone number and coordinates.

How would I be able to do delete Fred from this?

I can provide my full code if needed.

The solution for this is simple:

  1. read the file into an array
  2. remove the lines you want
  3. Write the content to a file (preferably a new one)

     #! /usr/bin/env ruby src='/tmp/data.src' dst='/tmp/data.new' content = File.readlines src content = content.grep_v /Fred;/ File.open(dst,'w+') {|f| f.puts content} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM