簡體   English   中英

Windows Batch-在txt文件中查找和編輯行

[英]Windows Batch - find & edit lines in txt file

我正在尋找一個簡單的腳本,該腳本將搜索文本文件(input.txt),其中列出的數字如下:

我基本上沒有批處理編碼(或與此有關的任何代碼)的經驗,希望這里有人可以幫助我。

0001
0002
0003
etc

我需要輸出(output.txt)為:

[test]0001[/test]
[test]0002[/test] 
[test]0003[/test] 
etc 

任何幫助,將不勝感激!

編輯:有人發布了python腳本,然后將其刪除,然后我才能對其進行測試並回復。 我知道這不是我最初要求的,但是效果很好! 謝謝!

如果有人找到此線程並可以使用它,就是這樣:

    rf = open("input.txt", 'r')
lines = rf.read().splitlines()
formatted_lines = ['[test]{}[/test]'.format(i) for i in lines]

with open('output.txt', 'w') as wf:
    for l in formatted_lines:
        wf.write("{}\n".format(l))

以下是從Windows命令提示符轉換數據的方法:

powershell -c "cat input.txt | %{\"[test]$_[/test]\"} > output.txt"

或者,如果您可以直接從PowerShell運行它,則可能是:

cat test.txt | %{"[test]$_[/test]"} > output.txt

這很容易,可以通過以下方式輕松完成:

@echo off

for /F "delims=" %%A IN (input.txt) do echo [test]%%A[/test]
pause>nul & exit /b 0

這樣,將在控制台中echo顯結果。 要將其保存到文件中,請使用:

@echo off

for /F "delims=" %%A IN (input.txt) do (echo [test]%%A[/test])>>output.txt
pause>nul & exit /b 0

echo並將其保存在文件中,請使用:

@echo off

for /F "delims=" %%A IN (input.txt) do (
    echo [test]%%A[/test]
    (echo [test]%%A[/test])>>output.txt
)

pause>nul & exit /b 0

注:提出在評論LotPings 有關的解決方案是相同的。 它適合於不發送整個for循環的輸出,而是發送echo命令的輸出,從而產生相同的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM