2017-04-21 99 views
2

我每天下載這些會計報告並將其格式化爲Quickbooks可導入的內容。如何將文本替換爲文本文件中的特定行?

Quickbooks使用3行標題,第一行數據是唯一的,它以字符串「TRNS」開始,所有以下數據行以「SPL」開頭。

我非常接近自動化這一點,但我在寫這將重命名「殺破狼」的批處理文件有困難第4行爲「緩曲」,沒有它取代「殺破狼」到「緩曲」的所有實例。

這是我基於本網站上的其他貼子。有沒有辦法讓它從第4行開始,只運行一次?

set "search=SPL" 
set "replace=TRNS" 
set "textfile=Input.txt" 
set "newfile=Output.txt" 
(for /f "delims=" %%i in (%textfile%) do (
    set "line=%%i" 
    setlocal enabledelayedexpansion 
    set "line=!line:%search%=%replace%!" 
    echo(!line! 
    endlocal 
))>"%newfile%" 

這裏是一個文件目前的樣子的圖像。

The circled text is what needs to be replaced

感謝

+2

請在文本文件中的文本* *複製到你的問題,而不是圖像,因爲這樣別人可以複製並測試他們的方法... – aschipfl

回答

0

一個簡單的行計數器應該有所幫助:

set "search=SPL" 
set "replace=TRNS" 
set "textfile=Input.txt" 
set "newfile=Output.txt" 
set lineNr=0 
(for /f "delims=" %%i in (%textfile%) do (
    set /a lineNr+=1 
    set "line=%%i" 
    setlocal enabledelayedexpansion 
    if !lineNr!==4 set "line=!line:%search%=%replace%!" 
    echo(!line! 
    endlocal 
))>"%newfile%" 
+0

完善。非常感謝你! –

0

你可以使用一個goto環和input redirection

@echo off 
rem // Do all operations in a sub-routine, redirect files once only here: 
< "Input.txt" > "Output.txt" call :SUB 
exit /B 

:SUB 
setlocal EnableDelayedExpansion 
rem /* The following `goto` loop is executed only until the matching line is found; 
rem everything beyond is handled by a single `findstr` command later: */ 
:LOOP 
rem // Read a single line from the input data: 
set "LINE=" & set /P LINE="" 
rem /* Terminate sub-routine if an empty line is encountered; 
rem this is needed to not have an infinite loop in case no line matches; 
rem data past that point are lost: */ 
if not defined LINE goto :EOF 
rem /* The following condition is fulfilled if the line begins with `SPL`; 
rem replace `if` by `if /I` to search case-insensitively: */ 
if "!LINE!"=="SPL!LINE:*SPL=!" (
    rem // Return the modified line, then continue below, leaving the loop: 
    echo(TRNS!LINE:*SPL=! 
) else (
    rem // Return the original line: 
    echo(!LINE! 
    rem // Jump to the beginning of the loop: 
    goto :LOOP 
) 
rem /* Return the remaining data; the very last line must be terminated by a 
rem line-break, otherwise `findstr` might hang indefinitely: */ 
findstr "^" 
endlocal 
goto :EOF 

A goto循環很慢,但是由於它只是在執行到匹配行之前執行,這是第四次,根據您的描述,整體方法比使用for /F循環更快,特別是在有很多因爲它們全部都是findstr而沒有任何解析。然而,最後一行必須以換行符結束;否則,findstr可能會掛起(取決於Windows版本)。

0

這是一個純粹的批處理解決方案,只要前4行是< = 1021個字符長,並以\ r \ n結尾(回車/換行)。我相信這是可能的最快的純批處理解決方案。

@echo off 
setlocal enableDelayedExpansion 
<input.txt >output.txt (
    for /l %%N in (1 1 3) do (
    set "ln=" 
    set /p "ln=" 
    echo(!ln! 
) 
    set "ln=" 
    set /p "ln=" 
    echo TRNS!ln:~3! 
    findstr "^" 
) 

如果你有JREPL.BAT,那麼你可以使用:

jrepl "^SPL" "TRNS" /inc 4 /f input.txt /o output.txt 

如果你想通過寫原文件,然後

jrepl "^SPL" "TRNS" /inc 4 /f input.txt /o - 

如果標題行的數量可能不同,並且您只想更改以SPL開頭的第一個遇到的線,然後

jrepl "^SPL" "skip=true;$txt=TRNS" /jq /f input.txt /o output.txt 

jrepl "^SPL" "TRNS" /exc "/^SPL/+1:-1" /f input.txt /o output.txt 
相關問題