我有一个用户名和数据库名称列表(如下所示)。第39行是“AHernandez Sample”。
这是我的脚本代码:
use strict;
use warnings;
my $i = 0;
my $n = 13;
while (<>) {
s/Sample /Sample Basic/ unless ++$i % $n;
print;
}
以下是我调用脚本的方法:perl Test1.txt&gt; Test3.txt。
这是奇怪的部分。第39行或第4行由代码修改。 Sample
变为Sample Basic
。然后发生每个第13个实例的预期行为。为什么第四个“样本”实例会被更改,然后每隔13个更改一次?
管理员
AEluf Bane
AEluf PoiznIvy
AEluf MrFreeze
AEluf演示
AEluf样品
AEluf Sample_U
AEluf Samppart
AEluf Sampeast
AEluf ASOsamp
AEluf PFPSamp
AEluf SampApp
AGodfrey Bane
AGodfrey PoiznIvy
AGodfrey MrFreeze
AGodfrey演示版
AGodfrey样品
AGodfrey Sample_U
AGodfrey Samppart
AGodfrey Sampeast
AGodfrey ASOsamp
AGodfrey PFPSamp
AGodfrey SampApp
AGold Bane
AGold PoiznIvy
AGold MrFreeze
AGold演示版
AGold样品
AGold Sample_U
AGold Samppart
AGold Sampeast
AGold ASOsamp
AGOLD PFPSamp
AGold SampApp
AHernandez Bane
AHernandez PoiznIvy
AHernandez MrFreeze
AHernandez演示
AHernandez Sample Basic
AHernandez Sample_U
AHernandez Samppart
AHernandez Sampeast
AHernandez ASOsamp
AHernandez PFPSamp
AHernandez SampApp
答案 0 :(得分:2)
您的脚本会尝试在文件的每个第13行替换s///
(13,26,39,52等)
[line: 1; ( 1 % 13)=> 1; try_replace: no] admin
[line: 2; ( 2 % 13)=> 2; try_replace: no] AEluf Bane
[line: 3; ( 3 % 13)=> 3; try_replace: no] AEluf PoiznIvy
[line: 4; ( 4 % 13)=> 4; try_replace: no] AEluf MrFreeze
[line: 5; ( 5 % 13)=> 5; try_replace: no] AEluf Demo
[line: 6; ( 6 % 13)=> 6; try_replace: no] AEluf Sample
[line: 7; ( 7 % 13)=> 7; try_replace: no] AEluf Sample_U
[line: 8; ( 8 % 13)=> 8; try_replace: no] AEluf Samppart
[line: 9; ( 9 % 13)=> 9; try_replace: no] AEluf Sampeast
[line:10; (10 % 13)=>10; try_replace: no] AEluf ASOsamp
[line:11; (11 % 13)=>11; try_replace: no] AEluf PFPSamp
[line:12; (12 % 13)=>12; try_replace: no] AEluf SampApp
[line:13; (13 % 13)=> 0; try_replace:yes] AGodfrey Bane
[line:14; (14 % 13)=> 1; try_replace: no] AGodfrey PoiznIvy
[line:15; (15 % 13)=> 2; try_replace: no] AGodfrey MrFreeze
[line:16; (16 % 13)=> 3; try_replace: no] AGodfrey Demo
[line:17; (17 % 13)=> 4; try_replace: no] AGodfrey Sample
[line:18; (18 % 13)=> 5; try_replace: no] AGodfrey Sample_U
[line:19; (19 % 13)=> 6; try_replace: no] AGodfrey Samppart
[line:20; (20 % 13)=> 7; try_replace: no] AGodfrey Sampeast
[line:21; (21 % 13)=> 8; try_replace: no] AGodfrey ASOsamp
[line:22; (22 % 13)=> 9; try_replace: no] AGodfrey PFPSamp
[line:23; (23 % 13)=>10; try_replace: no] AGodfrey SampApp
[line:24; (24 % 13)=>11; try_replace: no] AGold Bane
[line:25; (25 % 13)=>12; try_replace: no] AGold PoiznIvy
[line:26; (26 % 13)=> 0; try_replace:yes] AGold MrFreeze
[line:27; (27 % 13)=> 1; try_replace: no] AGold Demo
[line:28; (28 % 13)=> 2; try_replace: no] AGold Sample
[line:29; (29 % 13)=> 3; try_replace: no] AGold Sample_U
[line:30; (30 % 13)=> 4; try_replace: no] AGold Samppart
[line:31; (31 % 13)=> 5; try_replace: no] AGold Sampeast
[line:32; (32 % 13)=> 6; try_replace: no] AGold ASOsamp
[line:33; (33 % 13)=> 7; try_replace: no] AGold PFPSamp
[line:34; (34 % 13)=> 8; try_replace: no] AGold SampApp
[line:35; (35 % 13)=> 9; try_replace: no] AHernandez Bane
[line:36; (36 % 13)=>10; try_replace: no] AHernandez PoiznIvy
[line:37; (37 % 13)=>11; try_replace: no] AHernandez MrFreeze
[line:38; (38 % 13)=>12; try_replace: no] AHernandez Demo
[line:39; (39 % 13)=> 0; try_replace:yes] AHernandez Sample BasicBasic
[line:40; (40 % 13)=> 1; try_replace: no] AHernandez Sample_U
[line:41; (41 % 13)=> 2; try_replace: no] AHernandez Samppart
[line:42; (42 % 13)=> 3; try_replace: no] AHernandez Sampeast
[line:43; (43 % 13)=> 4; try_replace: no] AHernandez ASOsamp
[line:44; (44 % 13)=> 5; try_replace: no] AHernandez PFPSamp
[line:45; (45 % 13)=> 6; try_replace: no] AHernandez SampApp
替换每第n次出现的字符串:
while (<>) {
++$i if /Sample /;
s/Sample /Sample Basic/ unless $i % $n;
print;
}