如何删除ruby命令行中的字符

时间:2016-08-12 02:42:56

标签: ruby ruby-on-rails-4

我有一个.txt文件,其中包含我的电视频道播放列表的时间表。在文件中有许多字符为' logo_0 2 ' in。我运行ruby命令来处理播放列表(.txt文件)。我想如何删除所有' logo_0 2 '在红宝石?随附的是.txt文件和执行该过程的rb文件。我需要消除' logo_0 2 '在.txt文件中。

ITEM
preset_date 04/08/16
preset_time 22:09:00:00
title Awani (Comedy) Obviously Harith Iskander Sat Ep11 : Logo Inti College + Air Asia + Radio (E) Aug06 Sat 9.30pm
clip HUZKN01HM01AS
in_src 928500
out_src 929250
mode 1
no_vision_trans 1
no_audio_trans 1
type 1
logo_0 2
user_data 05_I_148671633
aspect_ratio 2
channel_id 2
use_subtitle 0
end
ITEM
preset_date 04/08/16
preset_time 22:09:30:00
title RiaHD (Series) Lelaki Macam Dia : Generic (Bm) Isnin - Khamis @ 2200
clip HUJST01HM01DV
in_src 1018125
out_src 1018875
mode 1
no_vision_trans 1
no_audio_trans 1
type 1
logo_0 2
user_data 05_I_148669553
aspect_ratio 2
channel_id 2
use_subtitle 0
end
ITEM
preset_date 04/08/16
preset_time 22:10:00:00
title Citra Mustika (Movie Compact) August Highlights (Bm)
clip HUXAP01HM01CM
in_src 962500
out_src 963250
mode 1
no_vision_trans 1
no_audio_trans 1
type 1
logo_0 2
user_data 05_I_148671344
aspect_ratio 2
channel_id 2
use_subtitle 0
end
ITEM
preset_date 04/08/16
preset_time 22:10:30:00
title Awani (News) Awani 7.45 (Bm) Setiap Hari @ 1945
clip HULHP01HM01AE
in_src 909000
out_src 909750
mode 1
no_vision_trans 1
no_audio_trans 1
type 1
logo_0 2
user_data 05_I_148671634
aspect_ratio 2
channel_id 2
use_subtitle 0
end
ITEM
preset_date 04/08/16
preset_time 22:11:00:00
title Episode Number 880
clip TAPE_REPLACEMENT
in_src 0
out_src 12000
mode 1
no_vision_trans 1
no_audio_trans 1
type 1
logo_0 1
notes Isu utama harian dan mingguan dikupas dengan mendalam oleh ahli panel.
user_data 05_P_174282654
logo_data_0 0
channel_id 2
end
ITEM
preset_date 04/08/16
preset_time 22:19:00:00
title Awani (News) Buletin Awani Noon (Bm) Setiap Hari @ 1200
clip HUUIL01HM01AI
in_src 915000
out_src 915750
mode 1
no_vision_trans 1
no_audio_trans 1
type 1
logo_0 2
user_data 05_I_148671635
aspect_ratio 2
channel_id 2
use_subtitle 0
end
ITEM
preset_date 04/08/16
preset_time 22:19:30:00
title Sports Rio Olympics 2016 - Opening Ceremony (BM) Live Aug6 6.55am
clip HUUUY01HM01AB
in_src 904000
out_src 904750
mode 1
no_vision_trans 1
no_audio_trans 1
type 1
logo_0 2
user_data 05_I_148668511
aspect_ratio 2
channel_id 2
use_subtitle 0
end
ITEM
preset_date 04/08/16
preset_time 22:20:00:00
title Awani (Biz News) Bisnes Awani Plus (Bm) Sabtu @ 1830
clip HPVAP01HM01DU
in_src 1050000
out_src 1050750
mode 1
no_vision_trans 1
no_audio_trans 1
type 1
logo_0 2
user_data 05_I_148671638
aspect_ratio 2
channel_id 2
use_subtitle 0
end

1 个答案:

答案 0 :(得分:0)

如果你分享你的ruby代码会很好,我们可以告诉你如何更新它。

filename = 'item.txt'
contents=File.read(filename)
contents.gsub!("logo_0 2\n", '')
File.open(filename, "w") { |f| f << contents }

如果您只想替换字符contents.gsub!("logo_0 2", ''),请使用此语句代替contents.gsub!("logo_0 2\n", '')