Board logo

标题: [文本处理] 批处理或VBS如何替换指定行内容? [打印本页]

作者: qq866286    时间: 2019-11-22 23:29     标题: 批处理或VBS如何替换指定行内容?

比如1.txt内容如下
aaa
bbb
ccc
ddd
eee
我想替换第二行内容为222,第五行内容为555.无论自身1.txt的2和5行是任何内容都替换,直接替换整行。
VBS或者bat都行,求大神,网上找个替换1行的,2行就不会了!下面是网上1行的,有没有大神给写个VBS或者bat能替换2行或者3行的,最好带注释哦
@echo off
set file=a.txt
(for /f "tokens=1* delims=:" %%a in ('findstr /n . "%file%"') do (
    if %%a equ 2 (echo,222) else echo,%%b
))>tmp
move tmp "%file%
作者: terse    时间: 2019-11-23 02:03

  1. @echo off
  2. set file=a.txt
  3. set "_2=222"
  4. set "_5=555"
  5. setlocal enabledelayedexpansion
  6. (for /f "tokens=1* delims=:" %%a in ('findstr /n . "%file%"') do (
  7.     if defined _%%a (echo;!_%%a!) else echo;%%b
  8. ))>tmp
  9. move tmp "%file%
复制代码

作者: qq866286    时间: 2019-11-23 10:23

回复 2# terse


    感谢大神的辛苦付出。测试可用!




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2