bat脚本去除TXT内行第一个空格之前的内容
时间: 2023-12-10 10:38:14 浏览: 35
假设你要处理的文本文件名为 input.txt,可以使用以下的 batch 脚本来去除每行第一个空格之前的内容:
```
@echo off
setlocal enabledelayedexpansion
set input=input.txt
set output=output.txt
if exist %output% del %output%
for /f "tokens=* delims=" %%a in (%input%) do (
set line=%%a
set first=!line: =!
echo !first!>>%output%
)
echo Done!
```
这个脚本中,首先定义了输入文件名和输出文件名,然后通过 for 循环逐行读取输入文件。对于每一行,在变量中去掉第一个空格之前的内容,然后将结果写入输出文件。最后输出一个 "Done!" 的消息表示处理完成。
相关问题
bat脚本修改txt文档内容
您可以使用以下代码将bat脚本修改txt文档内容:
```
@echo off
setlocal enabledelayedexpansion
set file="example.txt"
set search="old text"
set replace="new text"
for /f "tokens=*" %%a in (%file%) do (
set line=%%a
set line=!line:%search%=%replace%!
echo !line!>>temp.txt
)
del %file%
ren temp.txt %file%
```
在这个示例中,我们将`example.txt`中的所有`old text`替换为`new text`。您可以将`example.txt`替换为您想要修改的文件名称,将`old text`替换为您想要查找并替换的文本,将`new text`替换为要替换的文本。最后,您可以使用`del`命令删除原始文件,并使用`ren`命令将临时文件重命名为原始文件。
bat脚本 去除 txt 中相同的文本
可以使用以下的脚本来去除 txt 文件中相同的行:
```
@echo off
setlocal enabledelayedexpansion
set "input_file=input.txt"
set "output_file=output.txt"
rem Read input file and remove duplicate lines
for /f "usebackq delims=" %%a in ("%input_file%") do (
if not defined line[%%a] (
set "line[%%a]=1"
echo %%a>>"%output_file%"
)
)
```
将 `input.txt` 替换为你要去重的文本文件名,将 `output.txt` 替换为输出文件名。这个脚本会读取 `input.txt` 文件,去除其中的重复行,并将结果输出到 `output.txt` 文件中。