解析和更改数字的脚本
在编辑特定类型的文件时,我经常使用数字,这大多是乏味的工作。 该文件的格式如下:
damagebase = 8.834
"abc_foo.odf" 3.77
"def_bar.odf" 3.77
"ghi_baz.odf" 3.77
"jkl_blah.odf" 4.05
...
对于编写解析此文件并让我以编程方式更改每个数字的脚本,您会建议什么?
语言:我使用 C#、一些 F#(菜鸟)和 Lua。 如果您建议正则表达式,您能否提供具体的正则表达式,因为我不熟悉它们?
I am working with numbers a lot when editing a particular type of file, and it's mostly tedious work. The file has a format like this:
damagebase = 8.834
"abc_foo.odf" 3.77
"def_bar.odf" 3.77
"ghi_baz.odf" 3.77
"jkl_blah.odf" 4.05
...
What would you recommend for writing a script that parses this and lets me programmatically change each number?
Language: i use C#, some F# (noob), and Lua. If you suggest regexes, could you provide specific ones as i am not familiar with them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Perl 非常适合做这样的事情。 这是一个 perl 脚本,可以完成您想要的操作。
用法:
Perl is pretty good for stuff like this. Here's a perl script that will do what you want.
Usage:
如果这确实是您想要做的,请使用 awk:
编辑:好吧,如果您想保留所描述的空白,这应该可以工作(尽管它变得不优雅)。
If that's really all you want to do, use awk:
EDITED: All right, if you want to keep the whitespace as you describe, this should work (although it's getting inelegant).
您可以像这样使用 AWK(请注意如何轻松转换格式),
我在此示例脚本中将第二列乘以
3.1
。请注意,要恢复格式,
printf 的开头插入了一个 TAB,
两个
sed
命令将您的格式相互转换为适合 AWK 命令的格式You can use AWK like this (note how the formatting was converted easily for the purpose),
I am multiplying the 2nd column by
3.1
in this sample script.Note that to restore your formatting,
there is a TAB inserted at the start of the printf and,
the two
sed
commands translate from-and-back your format to a suitable one for the AWK command您可以将非空格和平铺符匹配到 Double.Parse :
运行它给出
You can match runs of non-whitespace and punt to Double.Parse:
Running it gives
我尝试过
(感谢gbacon)
即使我输入了正确的数据,它也会返回“不匹配”。 为什么要这样做?
这是测试数据:
我的理论是,因为每个非标题行前面的空格是一个制表符(这里不会以这种方式显示),所以正则表达式不匹配。 如果您想知道,空白很重要。
I tried
(thanks gbacon)
and it comes back with "no match" even when i put in the right data. Why does it do this?
Here's the test data:
My theory is that because the whitespace preceding each non-header line is a tab (and it won't show up that way here), the regex doesn't match. In case you're wondering, the whitespace IS important.