Board logo

标题: [文本处理] [已解决]批处理如何在文本指定位置插入相同的内容(字符串)? [打印本页]

作者: wang6542975    时间: 2016-4-3 13:24     标题: [已解决]批处理如何在文本指定位置插入相同的内容(字符串)?

add action=lookup comment="" disabled=no dst-address=5.10.136.0/22 table=Route_ChinaTelecom
add action=lookup comment="" disabled=no dst-address=5.10.140.0/24 table=Route_ChinaTelecom
add action=lookup comment="" disabled=no dst-address=14.16.0.0/12 table=Route_ChinaTelecom
add action=lookup comment="" disabled=no dst-address=14.104.0.0/13 table=Route_ChinaTelecom

在以上文本的每一行内加入src-address=192.168.16.0/24,效果如下

add action=lookup comment="" disabled=no src-address=192.168.16.0/24 dst-address=5.10.136.0/22 table=Route_ChinaTelecom
add action=lookup comment="" disabled=no src-address=192.168.16.0/24 dst-address=5.10.140.0/24 table=Route_ChinaTelecom
add action=lookup comment="" disabled=no src-address=192.168.16.0/24 dst-address=14.16.0.0/12 table=Route_ChinaTelecom
add action=lookup comment="" disabled=no src-address=192.168.16.0/24 dst-address=14.104.0.0/13 table=Route_ChinaTelecom
作者: happy886rr    时间: 2016-4-3 13:40

回复 1# wang6542975
下载frhttp://batch-cn.qiniudn.com/tool/2.1.7.0424/fr.exe和批处理放一起。
  1. fr *.txt -s -r:"(disabled=no) (dst-address=)" /t:"\1 src-address=192.168.16.0/24 \2"
复制代码

作者: pcl_test    时间: 2016-4-3 13:43

第三方
  1. sed "s/\(.*disabled=no\)/\1 src-address=192.168.16.0\/24/" 1.txt|more
复制代码

作者: wang6542975    时间: 2016-4-3 13:49

回复 3# pcl_test


   谢谢老大,第三方确实可以,有没有直接BAT可以解决的办法。
作者: wang6542975    时间: 2016-4-3 13:49

回复 2# happy886rr


    谢谢大哥,确实可以。
作者: happy886rr    时间: 2016-4-3 14:40

本帖最后由 happy886rr 于 2016-4-3 20:07 编辑

回复 5# wang6542975
不推荐批处理解决文本替换。各种批处理方案都有一些缺点
  1. @echo off
  2. (for /f "delims=" %%a in ('type "test.txt"^|findstr /n .*') do (
  3. set "str=%%a"
  4. setlocal enabledelayedexpansion
  5. set "str=!str:*:=!"
  6. set "str=!str:no dst=no src-address=192.168.16.0/24 dst!"
  7. echo;!str!
  8. endlocal
  9. ))>$ &move $ "test.txt"
复制代码

作者: wang6542975    时间: 2016-4-3 19:50

回复 6# happy886rr


    非常感谢。
作者: 大王派我去巡山    时间: 2019-3-12 22:06

第三方
pcl_test 发表于 2016-4-3 13:43



我依葫芦画瓢 !

sed "a/\(.*</title>\)/\1 <bb>插入内容</bb>/" *.html|more

目的是 想在 把同目录下所有的html文件(utf-8格式且包括子目录) 标签 </title>前插入  <db>插入内容</db>
使之变成 :  <bb>插入内容</bb></title>

却没有成功! 运行一闪而过 !
我下载了sed64位的放在同目录!

望版主指教! 谢谢




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