Deleting a line in a text file

前端 未结 2 826
南笙
南笙 2021-01-27 09:25

Ok, I tried to delete lines that contain -- from a file. The file looks like this:

  -----------------------------------------------------------------
  Lorem ip         


        
相关标签:
2条回答
  • 2021-01-27 09:40

    As others said, you can do it with grep, but if you want to do it completely in Ruby, then look at this question, the first answer should help you.

    0 讨论(0)
  • 2021-01-27 09:40

    So whats going on?

    Something like this:

    line = "-----------------------------------\n"
     => "-----------------------------------\n"
    
    a = line
     => "-----------------------------------\n"
    
    line.delete a
     ArgumentError: invalid range "--
     " in string transliteration
    

    The error raised by the delete method is because dashes are used for special character-range syntax:

    "hellob".delete "a-e"
     => "hllo"
    

    Probably you want to be taking a different approach to solve your goal, but it is difficult to tell what you want, as you don't show what you want to do with the data once lines are removed. Simply processing the line variable just means you have an altered String at that point.

    0 讨论(0)
提交回复
热议问题