在文本文件中的某些位置添加字符串

时间:2015-09-01 05:14:24

标签: batch-file text edit

我有一个包含以下内容的文本文件:

  

-849.4471 1272.173 22.8698 0 0 -1 7.54979E-008 Fire_Esc_6 385 792 24 -1
  -837.0507 1270.862 28.1249 0 0 -1 7.54979E-008 Fire_Esc_6b 385 792 24 -1
  -837.0654 1270.879 24.09248 0 0 -1 7.54979E-008 Fire_Esc_6 385 792 24 -1

对于每一行,我都需要

  • setAttr "sth";添加到该行的开头
  • 在第一个和第二个数字之间添加sth
  • 删除从385到第
  • 行的所有内容

我是批量的初学者,不知道从哪里开始。非常感谢您给予的任何帮助。

我已经做过,直到这里得到了一些帮助:0

FOR /F "tokens=8* delims= " %%G IN (C:\Users\Sherlock\Documents\3DReaperDX\Frames\1.txt) DO ECHO set %%G >12.txt

1 个答案:

答案 0 :(得分:1)

既然你已经尝试过自己解决这个问题,我觉得向你展示我为你的剧本想象的东西会更好。

@echo off

:: If 12.txt exists, delete it. This way, the entire file will be recreated when the script is re-run.
:: (If you don't want this to happen and you just want new data added to the end of the file every
::  time the script is run, just delete this part.)
if exist 12.txt del 12.txt


:: An example line looks like this:
:: -849.4471 1272.173 22.8698 0 0 -1 7.54979E-008 Fire_Esc_6 385 792 24 -1

:: Iterate through each line in 1.txt, storing each space-delimited string in a unique variable
:: %%A: -849.4471
:: %%B: 1272.173
:: %%C: 22.8698
:: %%D: 0
:: %%E: 0
:: %%F: -1
:: %%G: 7.54979E-008
:: %%H: Fire_Esc_6
:: Since we don't care about anything after the eighth token, we can just ignore it
:: The redirection command is at the start of the line to avoid an extra space at the end of the line
for /f "tokens=1-8" %%A in (C:\Users\Sherlock\Documents\3DReaperDX\Frames\1.txt) do >>12.txt echo setAttr "sth"; %%A sth %%B %%C %%D %%E %%F %%G %%H

由于这个脚本太短了(它只有三行带有大量注释),你甚至可以从命令行运行这个单行代码:

for /f "tokens=1-8" %A in (C:\Users\Sherlock\Documents\3DReaperDX\Frames\1.txt) do >>12.txt echo setAttr "sth"; %A sth %B %C %D %E %F %G %H

这将创建包含内容

的文件12.txt
  

setAttr" sth&#34 ;; -849.4471 sth 1272.173 22.8698 0 0 -1 7.54979E-008 Fire_Esc_6
  setAttr" sth&#34 ;; -837.0507 sth 1270.862 28.1249 0 0 -1 7.54979E-008 Fire_Esc_6b
  setAttr" sth&#34 ;; -837.0654 sth 1270.879 24.09248 0 0 -1 7.54979E-008 Fire_Esc_6