2015-12-04 67 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

相关问题