我可以添加什么到这个脚本,这将导致它不打印出 txt 文件中的重复行
脚本是
class TestKeyword
file = File.new("test.txt", "r")
while (line = file.gets)
if line['MAY_DAY']
date = line[/\w+ +\d+ +\d+:\d+:\d+/]
puts "#{date}"
end
end
end
这是测试文件:
Oct 15 12:54:01 WHERE IS THE LOVIN MAY_DAY
Oct 16 23:15:44 WHAT THE HECK CAN I DO ABOUT IT HUMP_DAY
Oct 16 14:16:09 I LOVE MY BABY GIRL MAY_DAY
Oct 16 08:25:18 CAN WAIT UNTIL MY BABY RECOVERS CRYSTAL_WIFE
Oct 18 17:48:38 I HOPE HE STOP MESSING WITH THESE FOOLISH CHILDREN TONY_SMITH
Oct 19 05:17:58 GAME TIME GO HEAD AND GET ME MAY_DAY
Oct 20 10:23:33 GAMESTOP IS WHERE ITS AT GAME_DAY
Oct 21 03:54:27 WHAT IS GOING ON WITH MY LUNCH HUNGRY_MAN
Oct 15 12:54:01 WHERE IS THE LOVIN MAY_DAY
Oct 16 23:15:44 WHAT THE HECK CAN I DO ABOUT IT HUMP_DAY
Oct 16 14:16:09 I LOVE MY BABY GIRL MAY_DAY
Oct 16 08:25:18 CAN WAIT UNTIL MY BABY RECOVERS CRYSTAL_WIFE
Oct 18 17:48:38 I HOPE HE STOP MESSING WITH THESE FOOLISH CHILDREN TONY_SMITH
Oct 19 05:17:58 GAME TIME GO HEAD AND GET ME MAY_DAY
Oct 20 10:23:33 GAMESTOP IS WHERE ITS AT GAME_DAY
Oct 21 03:54:27 WHAT IS GOING ON WITH MY LUNCH HUNGRY_MAN
目前,当我执行脚本时,我得到以下信息(这是具有关键字“MAY_DAY”的行的日期和时间:
1: Oct 15 12:54:01
1: Oct 16 14:16:09
1: Oct 19 05:17:58
1: Oct 15 12:54:01
1: Oct 16 14:16:09
1: Oct 19 05:17:58
我需要的输出是:
1: Oct 15 12:54:01
1: Oct 16 14:16:09
1: Oct 19 05:17:58
哪个没有重复项