Check if file contains string

后端 未结 4 577
无人及你
无人及你 2021-01-31 15:40

So I found this question on here, but I\'m having an issue with the output and how to handle it with an if statement. This is what I have, but it\'s always saying that it\'s tru

4条回答
  •  星月不相逢
    2021-01-31 15:41

    if anyone is looking for a solution to display last line of a file where that string occurs just do

    File.readlines('dir/testfile.txt').select{|l| l.match /monitor/}.last 
    

    example

    file:

    monitor 1
    monitor 2
    something else
    

    you'll get

    monitor 2
    

提交回复
热议问题