Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 如何使用任何windows或unix/linux命令行脚本按范围递增文件中的文本?_Perl - Fatal编程技术网

Perl 如何使用任何windows或unix/linux命令行脚本按范围递增文件中的文本?

Perl 如何使用任何windows或unix/linux命令行脚本按范围递增文件中的文本?,perl,Perl,是否有任何windows或unix/linux命令行脚本可以做到这一点?例如,单词“boss”应该从1增加到60,然后再从1增加到60 我有一个文件,其中包含一个这样的文本列表 She is the new boss She is the new boss She is the new boss She is the new boss She is the new boss She is the new boss She is the new boss She is the new boss S

是否有任何windows或unix/linux命令行脚本可以做到这一点?例如,单词“boss”应该从1增加到60,然后再从1增加到60

我有一个文件,其中包含一个这样的文本列表

She is the new boss She is the new boss She is the new boss She is the new boss She is the new boss She is the new boss She is the new boss She is the new boss She is the new boss . . . 她是新老板 她是新老板 她是新老板 她是新老板 她是新老板 她是新老板 她是新老板 她是新老板 她是新老板 . . . 我希望输出为:

She is the new boss1 She is the new boss2 She is the new boss3 . . . She is the new boss60 She is the new boss1 She is the new boss2 She is the new boss3 . . . She is the new boss60 . . . 她是新老板 她是新老板 她是新老板 . . . 她是新老板 她是新老板 她是新老板 她是新老板 . . . 她是新老板 . . .
到目前为止,我使用这个perl-I.bak-ape“s/”old text“/$&.+$A/ge”input.txt&&del input.txt.bak重复递增,但它产生的输出不是我想要的。

此Linux命令可以执行以下操作:

perl -i.bak -ape "s/"old-text"/$&.++$A /ge" input.txt && del input.txt.bak
i=1&&f;做回显“$f$i”;i=$((i+1));已完成新建_file.txt
以下是一个awk:

$ echo "She is the new boss
She is the new boss
She is the new boss
She is the new boss
She is the new boss
She is the new boss
She is the new boss
She is the new boss" | awk -v max=3 '{cnt>=max ? cnt=1:cnt++; $NF=$NF cnt;} 1'
印刷品:

She is the new boss1
She is the new boss2
She is the new boss3
She is the new boss1
She is the new boss2
She is the new boss3
She is the new boss1
She is the new boss2
因此,只需将输入值
max
更改为60,就可以像您的示例所描述的那样工作


对于文件,只需执行以下操作:

$ awk -v max=60 '{cnt>=max ? cnt=1:cnt++; $NF=$NF cnt;} 1' your_file
boss
周围添加了单词边界
\b
,以防止匹配
bossy
浮雕


最初,您使用了
$&.+$A

  • 递增的变量需要有条件地重置。可以使用一种方法来实现这一点
  • $&
    可以使用删除

在您知道所应用的处理是正确的之前,不使用save-in-place
-i
选项更容易。

我认为python在这个任务中不是干净的。但这是我为你写的

import re


class LineReplacer(object):
    def __init__(self, replace_word, max_word_count):
        self.word_index = 1
        self.word_to_replace = replace_word
        self.max_word_count = max_word_count

    @property
    def word_regex(self):
        return re.compile(f"(?<=[^a-zA-Z0-9]){self.word_to_replace}(?=[^a-zA-Z0-9])")

    def replace_function(self, match):
        current_index = self.word_index
        if self.word_index < self.max_word_count:
            self.word_index += 1
        else:
            self.word_index = 1
        return f"{match.group()}{current_index}"

    def replace_file(self, input_file_path, output_file_path):
        output = open(output_file_path, "w")
        with open(input_file_path, "r") as f:
            for line in f.readlines():
                new_line = self.word_regex.sub(self.replace_function, line)
                output.write(new_line)
        output.close()


if __name__ == "__main__":
    replacer = LineReplacer("boss", 60)
    replacer.replace_file("test.txt", "output.txt")
重新导入
类LineReplacer(对象):
定义初始(自身、替换字、最大字数):
self.word\u索引=1
self.word\u to\u replace=替换单词
self.max\u word\u count=max\u word\u count
@财产
def word_regex(self):
返回重新编译(f)(?
perl-ple'$.=($-1)%60+1'
$。
是当前行号


这可能会对您有所帮助。欢迎使用Stack Overflow。这是一个面向专业和热情程序员的问答页面。请在问题中添加您自己的代码。您至少需要展示您自己为解决此问题所做的研究量。如何对文件中的文本执行此操作?@dawg
import re


class LineReplacer(object):
    def __init__(self, replace_word, max_word_count):
        self.word_index = 1
        self.word_to_replace = replace_word
        self.max_word_count = max_word_count

    @property
    def word_regex(self):
        return re.compile(f"(?<=[^a-zA-Z0-9]){self.word_to_replace}(?=[^a-zA-Z0-9])")

    def replace_function(self, match):
        current_index = self.word_index
        if self.word_index < self.max_word_count:
            self.word_index += 1
        else:
            self.word_index = 1
        return f"{match.group()}{current_index}"

    def replace_file(self, input_file_path, output_file_path):
        output = open(output_file_path, "w")
        with open(input_file_path, "r") as f:
            for line in f.readlines():
                new_line = self.word_regex.sub(self.replace_function, line)
                output.write(new_line)
        output.close()


if __name__ == "__main__":
    replacer = LineReplacer("boss", 60)
    replacer.replace_file("test.txt", "output.txt")