Board logo

标题: [文本处理] 批处理如何隔几行插入固定内容? [打印本页]

作者: surain    时间: 2018-11-24 20:49     标题: 批处理如何隔几行插入固定内容?

求助,需要批量处理,A变成B这样的。

A.txt
1
00:00:02,299 --> 00:00:04,830
hey


2
00:00:04,830 --> 00:00:06,569
lab
实验室

3
00:00:06,569 --> 00:00:08,130
our
我们

4
00:00:08,130 --> 00:00:10,650
seek
寻求



B.txt
1
00:00:02,299 --> 00:00:04,830
{\fn微软雅黑}hey
{\fnArial}嘿

2
00:00:04,830 --> 00:00:06,569
{\fn微软雅黑}lab
{\fnArial}实验室

3
00:00:06,569 --> 00:00:08,130
{\fn微软雅黑}our
{\fnArial}我们

4
00:00:08,130 --> 00:00:10,650
{\fn微软雅黑}seek
{\fnArial}寻求
作者: yhcfsr    时间: 2018-11-24 22:07

  1. @echo off&setlocal enabledelayedexpansion
  2. (for /f "tokens=1* delims=:" %%a in ('findstr /n ".*" "A.txt"') do (
  3. set /a i+=1
  4. set/a "n=(i-3)%%5"
  5. if !n!==0 (echo;{\fn微软雅黑}%%b) else (
  6. set/a "n=(i-4)%%5"
  7. if !n!==0 (echo;{\fnArial}%%b) else echo;%%b
  8. )
  9. ))>B.txt
复制代码

作者: flashercs    时间: 2018-11-24 22:11

字体好像弄反了吧,中文用微软雅黑
修改内容.bat
  1. @echo off
  2. set "file1=A.txt"
  3. set "file2=B.txt"
  4. powershell -Command "$sr=[System.IO.StreamReader]::new([System.IO.FileStream]::new('%~dp0%file1%',[System.IO.FileMode]::Open,[System.IO.FileAccess]::Read),[System.Text.Encoding]::Default);$sw=[System.IO.StreamWriter]::new([System.IO.FileStream]::new('%~dp0%file2%',[System.IO.FileMode]::Create,[System.IO.FileAccess]::Write) ,[System.Text.Encoding]::Default);$n=0;while(!$sr.EndOfStream){$s=$sr.ReadLine();if($n -eq 2){$sw.WriteLine('{\fnArial}'+$s)}elseif($n -eq 3){$sw.WriteLine('{\fn微软雅黑}'+$s)}else{$sw.WriteLine($s)}if($n -eq 4){$n=0;$sw.Flush()}else{$n++}}$sw.Flush();$sr.Close();$sw.Close()"
  5. pause
  6. exit /b
复制代码

作者: surain    时间: 2018-11-24 23:14

回复 2# yhcfsr

结果是这个
   
X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}1 )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}1 )  else echo;1
)
)
1

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}00:00:02,299 --> 00:00:04,830 )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}00:00:02,299 --> 00:00:04,830 )  else echo;00:00:02,299 --> 00:00:04,830
)
)
00:00:02,299 --> 00:00:04,830

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}hey )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}hey )  else echo;hey
)
)
{\fn寰蒋闆呴粦}hey

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}嘿 )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}嘿 )  else echo;嘿
)
)
{\fnArial}嘿

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦} )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial} )  else echo;
)
)


X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}2 )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}2 )  else echo;2
)
)
2

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}00:00:04,830 --> 00:00:06,569 )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}00:00:04,830 --> 00:00:06,569 )  else echo;00:00:04,830 --> 00:00:06,569
)
)
00:00:04,830 --> 00:00:06,569

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}lab )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}lab )  else echo;lab
)
)
{\fn寰蒋闆呴粦}lab

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}实验室 )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}实验室 )  else echo;实验室
)
)
{\fnArial}实验室

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦} )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial} )  else echo;
)
)


X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}3 )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}3 )  else echo;3
)
)
3

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}00:00:06,569 --> 00:00:08,130 )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}00:00:06,569 --> 00:00:08,130 )  else echo;00:00:06,569 --> 00:00:08,130
)
)
00:00:06,569 --> 00:00:08,130

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}our  )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}our  )  else echo;our  
)
)
{\fn寰蒋闆呴粦}our

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}我们 )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}我们 )  else echo;我们
)
)
{\fnArial}我们

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦} )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial} )  else echo;
)
)


X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}4 )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}4 )  else echo;4
)
)
4

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}00:00:08,130 --> 00:00:10,650 )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}00:00:08,130 --> 00:00:10,650 )  else echo;00:00:08,130 --> 00:00:10,650
)
)
00:00:08,130 --> 00:00:10,650

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}seek )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}seek )  else echo;seek
)
)
{\fn寰蒋闆呴粦}seek

X:\DesktopX\测试>(
set /a i+=1  
set/a "n=(i-3)%5"  
if !n! == 0 (echo;{\fn寰蒋闆呴粦}寻求 )  else (
set/a "n=(i-4)%5"  
if !n! == 0 (echo;{\fnArial}寻求 )  else echo;寻求
)
)
{\fnArial}寻求

作者: yhcfsr    时间: 2018-11-25 00:17

回复 4# surain


    一看就知道你的脚本编码不是ANSI的,自己改成ANSI。
作者: surain    时间: 2018-11-25 11:00

回复 5# yhcfsr


    好的,已解决了,谢谢




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