2015-12-04 68 views
0

後更換數據我有一個包含各種字符串文本文件「info.txt」。 我想找到字符串「‘masterstring’:」第一次出現 並更換後一切「‘masterstring’:」用「替換數據文本」,但只有直到下一個逗號, 是什麼做的最快方法這與一個.bat腳本?字符串,字符串,直到下一個逗號

+0

我的第一反應是看看perl的。你可以寫一個正則表達式替換某些文本,並且速度很快 – lordkain

+0

到目前爲止你嘗試了什麼?你知道命令'find'或'findstr',還有'for/F'嗎?在命令提示符下鍵入命令後加'/?'以獲取詳細信息... – aschipfl

+0

通常,您可以想出某事。像'\「masterstring \」:([^,] *)'即,匹配「masterstring」和冒號字面上,然後捕捉一切到一個組中的逗號。查看[this regex101 fiddle](https://regex101.com/r/rF4aV3/1)查看工作演示。然而,這看起來像是一個'json'文件,所以也許有更好的方法,例如解析和替換。 – Jan

回答

1

您可以編寫一個.VBS腳本來執行此操作,但是使用JScript更容易,因爲批處理和JScript代碼可以在具有.BAT擴展名的相同混合文件中組合使用。我複製瞭解決方案this post,並稍微修改了該請求。

@set @a=0 /* 
@cscript //nologo //E:JScript "%~F0" <input.txt> output.txt 
@goto :EOF */ 

WScript.Stdout.Write(WScript.StdIn.ReadAll().replace(/masterstring.*,/,"masterstring replacement data text,")); 

您還沒有發佈樣本數據,所以我創建了一個簡單的輸入文件:

First line in data file. 
First line with masterstring and data to be replaced, preserve text after comma. 
Other line with masterstring and other data, that must not be changed. 
Last line in file. 

這是輸出:

First line in data file. 
First line with masterstring replacement data text, preserve text after comma. 
Other line with masterstring and other data, that must not be changed. 
Last line in file. 

有關此方法的進一步詳情,請參閱上面的鏈接以及該帖子中的鏈接。您也可以在本網站中搜索「jscript替換」和「jscript混合」,使用batch-file標籤。

+0

謝謝。這是正確的道路,但我有一些問題。我如何找到以下內容,並加上引號:「masterstring」:「 – user3552978

+0

您應該使用_code tags_清楚地分隔您的示例。是否」masterstring「:」'或'「masterstring」:'?任何其他?請根據這種輸入發佈一個簡短的輸入示例和所需的輸出;否則這只是一個猜謎遊戲...編輯你的問題(不要在評論中發佈數據)並使用代碼標籤! – Aacini

相關問題