我有一个包含以下内容的文件,
.
.
.
:job details
:emp details
:non tech emp details #line number 75
:assist details #line number 76
.
.
.
我试图在第76行添加新条目,并期望输出低于,
.
.
.
:job details
:emp details
:non tech emp details #line number 75
:tech emp details #line number 76
:assist details #line number 77
.
.
.
以下是我正在尝试的批处理脚本方法,
@echo off
setlocal enableextensions disabledelayedexpansion
set "seventysixthLine=:tech emp details"
(for /f "delims=" %%a in (C:\Users\username\Documents\emp_details.txt) do (
echo(%%a
if defined seventysixthLine (
echo(%seventysixthLine%
set "seventysixthLine="
)
)) > C:\Users\username\Documents\emp_details.txt
并且它不起作用,低于错误,
The system cannot find the file C:\Users\username\Documents\emp_details.txt.
任何帮助请...
答案 0 :(得分:3)
在PowerShell中你可以这样做:
$tmp = Get-Content 'C:\Users\username\Documents\emp_details.txt'
$tmp[0..75], ':tech emp details', $tmp[76.. ($tmp.Count -1)] |
Set-Content 'C:\Users\username\Documents\emp_details.txt'
注意:您可能不想在Set-Content
cmdlet中指定编码。
答案 1 :(得分:2)
逐行读取,将行写入新文件;如果该行是/包含您的搜索行,则在处理下一个输入行之前添加另一行:
@echo off
setlocal enableextensions disabledelayedexpansion
set "searchstring=:non tech emp details"
set "includestring=:tech emp details"
(for /f "delims=" %%a in (t.txt) do (
echo(%%a
REM alternative; use one of the two below lines:
rem if "%%a"=="%searchstring%" echo(%includestring%
echo %%a|findstr /b /c:"%searchstring%" >nul && echo(%includestring%
)) > t.tmp
move t.tmp t.txt
注意:空行将被忽略(删除)。可以在需要时使用更多代码进行调整。
(我猜,不依赖行号)