`

Replace text in file

阅读更多

 

Windows

 

@echo off

if %1%.==. (
  echo Usage: %0% FILE_FULL_PATH SOURCE_STRING DESTINATION_STRING
  echo Where: FILE_FULL_PATH is the full path of the file to be replaced in
  echo        SOURCE_STRING is string to be replaced
  echo        DESTINATION_STRING is string to be replaced as
  pause 
  exit
)

if %2%.==. (
  echo Usage: %0% FILE_FULL_PATH SOURCE_STRING DESTINATION_STRING
  echo Where: FILE_FULL_PATH is the full path of the file to be replaced in
  echo        SOURCE_STRING is string to be replaced
  echo        DESTINATION_STRING is string to be replaced as
  pause 
  exit
)

if %3%.==. (
  echo Usage: %0% FILE_FULL_PATH SOURCE_STRING DESTINATION_STRING
  echo Where: FILE_FULL_PATH is the full path of the file to be replaced in
  echo        SOURCE_STRING is string to be replaced
  echo        DESTINATION_STRING is string to be replaced as
  pause 
  exit
)

setlocal enabledelayedexpansion 

set file=
rem set /p replaced= Please input the file full path: 
set "file=%1%"
for %%i in ("%file%") do set file=%%~fi 
echo. 

set replaced= 
rem set /p replaced= Source String: 
set "replaced=%2%"  
echo. 

set all= 
rem set /p all= Destination String: 
set "all=%3%"
echo.

for /f "delims=" %%i in ('type "%file%"') do ( 
  set str=%%i 
  set "str=!str:%replaced%=%all%!" 
  echo !str!>>"%file%".tmp 
) 

copy "%file%" "%file%".bak >nul 2>nul 
move "%file%".tmp "%file%" 
rem start "" "%file%" 

 

 

Linux

 

sed -in-place -e 's/old/new/g' text.txt

分享到:
评论

相关推荐

    Guest_replace_File.rar_python多线程

    def replace_text(file_path, old_text, new_text): # 在这里实现替换文本的逻辑 ... threads = [] for file in file_list: t = threading.Thread(target=replace_text, args=(file, old_text, new_text)) ...

    replace-in-files-cli:替换文件中匹配的字符串和正则表达式

    $ npm install --global replace-in-files-cli 用法 $ replace-in-files --help Usage $ replace-in-files Options --regex Regex pattern to find (Can be set multiple times) --string String to find ...

    replace-in:替换给定文件中的文本

    npm install replace-in 这个怎么运作? 它创建一个读取流以分块的形式从目标文件中读取。 替换每个请求并使用写入流写入结果。 更换完成后,将返回最终报告。 原料药 替换(选项) options对象包含: path: ...

    Galcott.Super.Text.Search.v3.13-BEAN

    In plain ASCII text files it also allows you to replace text. You can search anything from a single file to an entire drive for any text you need to find. Both plain text files (program source code, ...

    Python代码源码-实操案例-框架案例-如何批量替换Word文档中的指定内容(包括页眉).zip

    def replace_in_paragraphs(doc, old_text, new_text): for paragraph in doc.paragraphs: paragraph.text = paragraph.text.replace(old_text, new_text) ``` 3. **处理页眉**: 页眉是文档的一部分,可以...

    UE(官方下载)

    The benefit of a column maker is that it can help you to format your text/code, or in some cases to make it easier to read in complex nested logic. Quick Open UltraEdit and UEStudio provide multiple ...

    Text_class.rar

    with open('text_file.txt', 'r') as file: content = file.read() ``` 这段代码会打开名为`text_file.txt`的文件,并将其内容读取到变量`content`中。如果文件位于一个子目录下,我们需要提供完整的路径,例如`'...

    c#修改text文件

    string newName = $"{file.Name.Replace($"." + oldSuffix, $"." + newSuffix)}"; File.Copy(file.FullName, Path.Combine(dir.FullName, newName), true); } } static void Main() { // 示例调用 ...

    Text Processing in Python

    对于大文件,使用`for line in file:`迭代器可以逐行读取,以节省内存。 Python的`string`模块包含许多预定义的常量和函数,如标点符号集合和各种字符串转换函数。`str.lower()`、`str.upper()`和`str.capitalize()...

    VBNET逐行读写文本文件

    For Each textLine In lines ' lines 是包含要写入文本的数组 File.WriteLine(fileNumber, textLine) Next Finally FileClose(fileNumber) End Try ``` 特别地,如果你想要避免在写入文本时包含双引号,你可以...

    Sublime Text 3 (Build 3143)

    Find in Files panel now responds to find_all and replace_all commands Various regex handling improvements in the Find panel Fixed text widgets cutting off the bottom pixel of their selection border ...

    强大的免费的十六进制编辑器

    - Replace text or hex string, e.g. replace "0D 0A" by "0A" or replace "0D 0A" by text "EOL" - Extremely fast "replace all" mode (if needed, additional memory is allocated beforehead, not at every ...

    MySQL数据导入导出方法与工具mysqlimport.pdf

    3. 利用LOAD DATA INFILE导入数据:在数据库中,执行SQL命令,如`LOAD DATA INFILE '/path/to/file.csv' INTO TABLE table_name FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';`。 知识点7:mysqlimport的...

    mysqlloaddatainfile的用法.pdf

    LOAD DATA INFILE "d:/Websites/Sxxxx/test1.txt" REPLACE INTO TABLE `names` FIELDS TERMINATED BY ',' ENCLOSED BY '"'; ``` #### 五、远程客户端导入与本地服务器导入的区别 1. **远程客户端导入**: 需要在 ...

    cmake之file命令:文件处理命令

    file(READ <filename> <out-var> [OFFSET ] [LIMIT <max-in>] [HEX]) ``` **描述**: - 读取文件 `<filename>` 的内容,并将其保存到变量 `<out-var>` 中。 - `OFFSET` 和 `LIMIT` 参数可选,分别指定从文件中的...

    基于Python实现替换word中的文字

    replace_text_in_word('your_file.docx', '旧的文字', '新的文字') ``` 在这个例子中,`replace_text_in_word`函数接受三个参数:Word文档的路径,要替换的旧文本,以及替换后的新文本。函数遍历文档中的每个段落,...

    python 自动办公- Python替换不了word中的文字?.zip

    def replace_text_in_docx(file_path, old_text, new_text): doc = Document(file_path) for paragraph in doc.paragraphs: for run in paragraph.runs: run.text = run.text.replace(old_text, new_text) doc....

    Sublime Text Build 3124 x64 Setup.exe

    Fixed Replace not working as expected in conjunction with regex look behinds Fixed build systems being unable to use "file_patterns" with the exec command Corrected tab overlap on HiDPI Windows and ...

    Oracle P/L SQL实现FTP上传、下载功能

    ai_LocalFilename in out Nocopy UTL_File.File_Type, as_TransferMethod in VarChar2 Default Null ); Procedure p_GetFileList( ac_Connection in out Nocopy Connection, afl_List out ...

Global site tag (gtag.js) - Google Analytics