I am currently trying to delete a variable from a batch file which is in a text file
我目前正在尝试从文本文件中的批处理文件中删除变量
e.g. delete %variable%
例如删除%变量%
where %variable% = "test"
其中%variable%=“test”
So the batch script would delete the instance of "test" in the specified text file
因此批处理脚本将删除指定文本文件中的“test”实例
Can this be done?
可以这样做吗?
2 个解决方案
#1
0
@echo off
setlocal enabledelayedexpansion
set variable=test
for /f "delims=" %%l in (foo.txt) do (
set "line=%%f"
echo !line:%variable%=!
)
#2
0
To avoid potential special character issues with the string you are searching for, it may be better to just call findstr directly like this:
为了避免您正在搜索的字符串存在潜在的特殊字符问题,最好直接调用findstr,如下所示:
type input.txt | findstr /b /e /v "remove_line" > input_without_remove_line.txt
/b and /e together will only match if the "remove_line" is the only text on a line. /v will switch the output to only print all lines that do not match.
如果“remove_line”是一行中的唯一文本,则/ b和/ e将仅匹配。 / v将切换输出仅打印所有不匹配的行。
If you are sure you're not passing special characters, it is pretty easy to wrap that in a small batch and replace remove line with %1 or %* to use your passed parameters.
如果您确定没有传递特殊字符,则很容易将其包装在一个小批量中,并用%1或%*替换删除行以使用传递的参数。
Unfortunately, you will need to use a temporary file - replacing the file in place doesn't work with the DOS output redirection.
不幸的是,您将需要使用临时文件 - 替换该文件不适用于DOS输出重定向。
If you were wanting to delete all instances of a specified word within any line, batch and findstr are probably not the way to do it. Look at sed, which can do much more and is freely available. If you can't install another utility, even vbscript using cscript would be a better way to do this than batch.
如果您想要删除任何行中的指定单词的所有实例,则批处理和findstr可能不是这样做的方法。看看sed,它可以做更多,可以免费使用。如果你不能安装另一个实用程序,即使使用cscript的vbscript也是比批处理更好的方法。
#1
0
@echo off
setlocal enabledelayedexpansion
set variable=test
for /f "delims=" %%l in (foo.txt) do (
set "line=%%f"
echo !line:%variable%=!
)
#2
0
To avoid potential special character issues with the string you are searching for, it may be better to just call findstr directly like this:
为了避免您正在搜索的字符串存在潜在的特殊字符问题,最好直接调用findstr,如下所示:
type input.txt | findstr /b /e /v "remove_line" > input_without_remove_line.txt
/b and /e together will only match if the "remove_line" is the only text on a line. /v will switch the output to only print all lines that do not match.
如果“remove_line”是一行中的唯一文本,则/ b和/ e将仅匹配。 / v将切换输出仅打印所有不匹配的行。
If you are sure you're not passing special characters, it is pretty easy to wrap that in a small batch and replace remove line with %1 or %* to use your passed parameters.
如果您确定没有传递特殊字符,则很容易将其包装在一个小批量中,并用%1或%*替换删除行以使用传递的参数。
Unfortunately, you will need to use a temporary file - replacing the file in place doesn't work with the DOS output redirection.
不幸的是,您将需要使用临时文件 - 替换该文件不适用于DOS输出重定向。
If you were wanting to delete all instances of a specified word within any line, batch and findstr are probably not the way to do it. Look at sed, which can do much more and is freely available. If you can't install another utility, even vbscript using cscript would be a better way to do this than batch.
如果您想要删除任何行中的指定单词的所有实例,则批处理和findstr可能不是这样做的方法。看看sed,它可以做更多,可以免费使用。如果你不能安装另一个实用程序,即使使用cscript的vbscript也是比批处理更好的方法。