标题: [文本处理] 批处理如何将文本中的指定内容替换成某一字符? [打印本页]
作者: ndyo 时间: 2016-1-18 17:21 标题: 批处理如何将文本中的指定内容替换成某一字符?
比如有一批文本文件,内容类似与: 哈哈哈:<sdfsdf>嗯嗯</sfsf><sfsdaf>好哈哈哈哈</lwioutew><dsafasdf>46546465465</ldsjfosjf><fhsafsaj>6464654654678</sdfsafj>
现在想把所有文本文件里的尖括号及其内的内容批量替换成某一字符,比如 |,该怎样写命令?
作者: /zhqsystem/zhq 时间: 2016-1-20 14:01
本帖最后由 /zhqsystem/zhq 于 2016-1-20 14:16 编辑
回复 1# ndyo
- @echo off&(setlocal enabledelayedexpansion)>nul 2>nul
- for /f "delims=" %%i in ('type ".\1.txt"')do (
- for /f "delims=" %%j in ("%%i")do (set "#name#=%%j"
- for /l %%k in (0,1,8192)do if /i "!#name#:~%%k,1!"=="<" set/a "m+=1"
- for /l %%k in (0,1,8192)do if /i "!#name#:~%%k,1!"==">" set/a "n+=1"
- if /i "!m!"=="!n!" (
- for /l %%k in (1,1,!m!)do (
- if not defined #name_l# for /f "delims=<" %%l in ("%%j")do set "#name_l#=%%l"
- for /f "tokens=1,* delims=<" %%l in ("!#name#!")do (set "#name#=%%m"
- for /f "tokens=1,* delims=<" %%n in ("!#name#!")do (set "#name_n#=%%n"
- if /i "!#name_n#:~0,1!"=="/" (
- set "#name_new#=!#name_new#!bathome.net^<%%n"
- )else (
- set "#name_new#=!#name_new#!^<%%n"
- )
- set "#new#=!#name_l#!bathome.net!#name_new#!"
- )
- )
- )
- )else (
- (echo,自动应答错误原始值:%%j)
- )
- set "#new#=!#new#!"
- if defined #new# (
- (echo,!#new#!)
- )
- set "#name_l#="&set "#name_new#="&set "new="&set "m="&set "n="
- echo/
- )
- )
- pause
复制代码
- @echo off&(setlocal enabledelayedexpansion)>nul 2>nul
- for /f "delims=" %%i in ('type ".\1.txt"')do (
- for /f "delims=" %%j in ("%%i")do (set "#name#=%%j"
- for /l %%k in (0,1,8192)do if /i "!#name#:~%%k,1!"=="<" set/a "m+=1"
- for /l %%k in (0,1,8192)do if /i "!#name#:~%%k,1!"==">" set/a "n+=1"
- if /i "!m!"=="!n!" (
- for /l %%k in (1,1,!m!)do (
- for /f "tokens=1,* delims=<" %%l in ("!#name#!")do (set "#name#=%%m"
- for /f "delims=>" %%l in ("!#name#!")do (set "#name_l#=%%l"
- if /i "!#name_l#:~0,1!"=="/" (
- set "new=!new!bathome.net^<%%l^>"
- )else (
- set "new=!new!^<%%l^>"
- )
- )
- )
- )
- )else (
- (echo,自动应答错误原始值:%%j)
- )
- if defined new (
- (echo,!new!)
- )
- set "new="&set "m="&set "n="
- echo/
- )
- )
- pause
复制代码
作者: pcl_test 时间: 2016-1-20 19:01
本帖最后由 pcl_test 于 2016-11-19 22:17 编辑
- mshta http://bathome.net/s/hta/ "type('a.txt').replace(/<[^>]*>/g,'|')"
复制代码
- sed "s/<[^>]*>/|/g" "a.txt"
复制代码
- powershell -c "(Get-Content 'a.txt') -replace '<[^>]*>','|'"
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |