【问题标题】:Adding characters to the end of multiple lines在多行末尾添加字符
【发布时间】:2014-06-26 05:21:25
【问题描述】:

我使用的是 Windows8。 所以,这就是我所拥有的:

hello

这就是我想要的:

hello1
hello2
hello3
hello4
hello5

但是有大约 1500 个数字,而且这个词不是你好。 我不是任何程序员,但我可以使用 Notepad++,这就是我想要使用的。 我不想做一千次find: $replace with: 1。 我已经搜索了一段时间的答案,有人可以帮助我吗?

【问题讨论】:

  • 您可能会在Super User 获得更好的答案。请务必提及您使用的操作系统。
  • 说明我使用的是什么操作系统 :)
  • 您要重命名文件夹名称吗?动态地,你可以使用 php
  • 不。这是一个文本文件。

标签: replace notepad++


【解决方案1】:

这可以在 Notepad++ 中借助插件实现。首先,从插件管理器安装 Python 脚本插件。接下来,使用以下代码从插件菜单创建一个新脚本:

# First we'll start an undo action, then Ctrl-Z will undo the actions of the whole script
editor.beginUndoAction()

# Initialize counter to zero
counter = 0

# Define function to append incremental value to end of search result
def increment_counter(m):
    try:
        global counter
        counter += 1
        return m.group(0) + str(counter)
    except:
        return m.group(0)

# Search for value and replace with with appended incremental result
editor.pyreplace('^hello', increment_counter)

# End the undo action, so Ctrl-Z will undo the above two actions
editor.endUndoAction()

接下来,为您要替换的文本修改“hello”文本并保存脚本。最后,当您在 Notepad++ 中选择文件时,运行新保存的脚本。请注意,如果结果不符合您的意愿,您可以撤消。

【讨论】:

    【解决方案2】:

    我知道你喜欢 Notepad++,我也喜欢,但我想知道你是否不能通过 PowerShell 更轻松地解决这个问题,你已经默认安装在 Windows 8 中。

    这个sn-p会在“hello”后面附加一个递增的数字,从1到3。你可以很容易地改变数量和单词。要运行此脚本,只需打开“开始”菜单并键入powershell,然后粘贴下面的行并单击绿色三角形(如果打开命令提示符,则按 Enter)。我建议你打开 PowerShell ISE 程序,但普通的 shell 也可以。

    1..3 | % { "hello{0}" -f $_ }
    

    如果你想把它保存到一个文件中,你可以运行它:

    1..3 | % { "hello{0}" -f $_ } > C:\hello.txt
    

    恭喜!您已经编写了您的第一个脚本!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 2023-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多