【问题标题】:How to replace certain string with set of string list?如何用一组字符串列表替换某些字符串?
【发布时间】:2014-10-09 06:46:58
【问题描述】:

我有一个这样的文件

"Hi, my name is Bob and I love blue."

我有一个类似这样的名称的 .txt:

Jane
Janet
Tim
etc.etc.

我想用名字替换 bob 的新行,就像这样:

"Hi, my name is Jane and I love blue."
"Hi, my name is Janet and I love blue."
"Hi, my name is Tim and I love blue."
etc. etc.

我该怎么做(在 Windows 上?)

【问题讨论】:

  • 你真的应该展示你自己解决这个问题的努力。就目前而言,它太宽泛了,因为我们不知道你被困在哪里。
  • 为此使用数据库会更好。您要问的内容太宽泛了,需要一种复杂的方法来从文本文件中提取名称,方法是根据文本文件的方式分解每个名称(作为新行、逗号分隔等) .从谷歌搜索“从文本文件替换 php 分配变量”开始,然后从那里开始。在 Stack 上找到的一个答案是 stackoverflow.com/a/20833952,您可以在其中开始摆弄。祝你好运。

标签: php windows text replace scripting


【解决方案1】:

您可以使用小批量脚本来实现它。见下文 -

代码 -

@echo off

set /p "var=" <input.txt
setlocal enabledelayedexpansion
for /f %%i in (names.txt) do (
 set name=%%i
 call set "output=%%var:bob=!name!%%
 echo.!output!
REM echo.!name!
)

这是输出 -

D:\Scripts>type input.txt
Hi, my name is Bob and I love blue.
D:\Scripts>
D:\Scripts>
D:\Scripts>
D:\Scripts>op.bat
Hi, my name is Jane and I love blue.
Hi, my name is Janet and I love blue.
Hi, my name is Tim and I love blue.

干杯,G

【讨论】:

  • 谢谢,我在手机上,但我会试试看!
猜你喜欢
  • 2023-03-04
  • 2018-08-23
  • 1970-01-01
  • 2013-07-09
  • 2021-07-16
  • 2012-03-30
  • 2011-02-21
  • 2022-01-02
  • 1970-01-01
相关资源
最近更新 更多