Batch file 替换逗号分隔的txt文件中的许多短语

Batch file 替换逗号分隔的txt文件中的许多短语,batch-file,replace,text-files,Batch File,Replace,Text Files,任何人都知道我不能一次换一句话吗? 假设我有一个包含短语的txt文件,该短语将被替换,例如: 111 , 232 122 , 324 等等 左侧的每个短语都需要替换为右侧的短语,并且每个新的替换都在一个新行中。是否有一种方法可以替换加载txt文件时的多个短语,而不是一个接一个令人沮丧的替换?根据您的情况,您可能希望以不同的方式处理此问题。如果适用于你的工作环境,我会接受雷汉的建议。如果适用,您也可以在vim中进行更换。如果您必须使用批处理脚本,这应该会有所帮助,尽管这是我不知道的,可能需要

任何人都知道我不能一次换一句话吗? 假设我有一个包含短语的txt文件,该短语将被替换,例如:

111 , 232


122 , 324
等等


左侧的每个短语都需要替换为右侧的短语,并且每个新的替换都在一个新行中。是否有一种方法可以替换加载txt文件时的多个短语,而不是一个接一个令人沮丧的替换?

根据您的情况,您可能希望以不同的方式处理此问题。如果适用于你的工作环境,我会接受雷汉的建议。如果适用,您也可以在vim中进行更换。如果您必须使用批处理脚本,这应该会有所帮助,尽管这是我不知道的,可能需要对您的部分进行一些调整,因为您没有windows机器来测试它:

setlocal enabledelayedexpansion
set new_file=your_new_or_temporary_file.txt
set name_of_your_txt_file_of_replacements=your_replacements_file_name.txt
set name_of_your_txt_file_of_content_to_replace=your_file_of_content_to_replace.txt

if EXIST "!new_file!" del "!new_file!"

for /f 'eol=; tokens=1,2 delims=,' %%l in ('type "!name_of_your_txt_file_of_replacements!"') do (
  set existing_text=%%l
  set replacement_text=%%i
  @REM: Based on your example you will have trailing and leading blanks
  @REM: See this site to trim your existing_text and replacement_text
  @REM: Source: http://www.dostips.com/DtTipsStringManipulation.php

  for /f "tokens=* delims= " %%a in ("!replacement_text!") do set replacement_text=%%a

  for /l %%a in (1,1,31) do (
    if "!existing_text:~-1!"==" " set existing_text=!existing_text:~0,-1!
    if "!replacement_text:~-1!"==" " set replacement_text=!replacement_text:~0,-1!
  )

  echo replacing !existing_text! with !replacement_text!...

  for /f 'eol=; tokens=1 delims=' %%c in ('type   "!name_of_your_txt_file_of_content_to_replace!"') do (
    set line=%%c
    for /f 'eol=; tokens=1 delims=' %%c in ('echo !line! ^| findstr /i /c:"!existing_text!"') do (        
      for /f 'eol=; tokens=1,2 delims=,' %%c in ("!existing_text!,!replacement_text!") do
      set line=!line:%%c=%%d!
    )
    @REM::Just reread your specs, and if you want the new file to just contain
    @REM::  the replacement text, then do this instead:
    @REM::  echo !replacement_text! >> !new_file!
    @REM::Otherwise, copy of existing file with updated text

    echo !line! >> !new_file!
  )
)

@REM:: Compare your original file with !new_file! and make sure it is all good.
@REM:: Once you have to code to where it is all good do this

mv "!new_file!" "!name_of_your_txt_file_of_content_to_replace!"

如果你遇到问题,请告诉我。祝你好运

你需要使用sed。为了告诉你确切的命令,我们需要更多的细节。像一个输入输出文件的示例。对不起。我想我不明白你需要什么。你说的是两个文件,一个是上面提到的多个替换短语,另一个是你想要处理的文本?如果是这样的话,那么每一个新的替代品在一个新行中意味着什么?按照Raihan的建议,发布一个示例输入和输出。