Vim 搜索和替换,添加常量

发布于 2024-10-02 07:34:21 字数 314 浏览 1 评论 0原文

我知道这是一个很遥远的事情,但我有一个巨大的文本文件,我需要将给定的数字添加到符合某些条件的其他数字中。

例如。

identifying text 1.1200
identifying text 1.1400

我想将其(通过添加 1.15)转换为

identifying text 2.2700
identifying text 2.2900

通常我会在 Python 中执行此操作,但它是在 Windows 计算机上,我无法安装太多东西。不过我有 Vim :)

I know this is a long shot, but I have a huge text file and I need to add a given number to other numbers matching some criteria.

Eg.

identifying text 1.1200
identifying text 1.1400

and I'd like to transform this (by adding say 1.15) to

identifying text 2.2700
identifying text 2.2900

Normally I'd do this in Python, but it's on a Windows machine where I can't install too many things. I've got Vim though :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不必了 2024-10-09 07:34:21

这是霍布斯解决方案的简化和修复:

:%s/identifying text \zs\d\+\(.\d\+\)\=/\=(1.15+str2float(submatch(0)))/

感谢 \zs,无需回忆前导文本。感谢 str2float() 对整个数字进行一次加法(换句话说,1.15 + 2.87 将给出预期结果,4.02,而不是 3.102)。

当然这个解决方案需要最新版本的 Vim(7.3?)

Here is a simplification and a fix on hobbs' solution:

:%s/identifying text \zs\d\+\(.\d\+\)\=/\=(1.15+str2float(submatch(0)))/

Thanks to \zs, there is no need to recall the leading text. Thanks to str2float() a single addition is done on the whole number (in other words, 1.15 + 2.87 will give the expected result, 4.02, and not 3.102).

Of course this solution requires a recent version of Vim (7.3?)

对风讲故事 2024-10-09 07:34:21

您可以执行捕获正则表达式,然后使用 vimscript 表达式作为替换,例如

:%s/\(identifying text \)\(\d\+\)\.\(\d\+\)/
  \=submatch(1) . (submatch(2) + 1) . "." . (submatch(3) + 1500)

(仅不带换行符)。

You can do a capturing regex and then use a vimscript expression as a replacement, something like

:%s/\(identifying text \)\(\d\+\)\.\(\d\+\)/
  \=submatch(1) . (submatch(2) + 1) . "." . (submatch(3) + 1500)

(only without the linebreak).

小鸟爱天空丶 2024-10-09 07:34:21

你的数字格式似乎是固定的,所以很容易转换为int并返回(删除点)添加11500并将点放回去。

:%s/\.//
:%normal11500^A " type C-V then C-a
:%s/....$/.&/

如果您不想在所有行上执行此操作,而只想在与“识别文本”匹配的行上执行此操作,请将所有 % 替换为“g/识别文本/”

Your number format seems to be a fixed one, so it's easy to convert to int and come back (remove the dot) add 11500 and put the dot back.

:%s/\.//
:%normal11500^A " type C-V then C-a
:%s/....$/.&/

If you don't want to do that on all the lines but only the one which match 'identifying text' replace all the % by 'g/indentifying text/'

表情可笑 2024-10-09 07:34:21

对于整数,您只需使用 n^A 将 n 添加到数字(并使用 n^X 减去它)。我怀疑这是否适用于小数。

For integers you can just use n^A to add n to a number (and n^X to subtract it). I doubt whether that works for fractional numbers though.

祁梦 2024-10-09 07:34:21

好吧,这可能不是 vim 的解决方案,但我认为 awk 可以提供帮助:

cat testscript | LC_ALL=C awk '{printf "%s %s %s %s %.3f\n", $1,$2,$3,$4,$5+1.567   }'

并且我需要 LC_ALL=C 的测试

this is a number 1.56
this is a number 2.56
this is a number 3.56

来正确转换浮点分隔符,也许有一个更优雅的解决方案来打印开头/其余部分字符串。结果如下:

this is a number 3.127
this is a number 4.127
this is a number 5.127

Well this might not be a solution for vim but I think awk can help:

cat testscript | LC_ALL=C awk '{printf "%s %s %s %s %.3f\n", $1,$2,$3,$4,$5+1.567   }'

and the test

this is a number 1.56
this is a number 2.56
this is a number 3.56

I needed the LC_ALL=C for the correct conversion of the floating point separator, and maybe there is a more elegant solution for printing the beginning/ rest of the string. And the result looks like:

this is a number 3.127
this is a number 4.127
this is a number 5.127
半边脸i 2024-10-09 07:34:21

请使用宏

qa .......................... start record macro 'a'
/iden<Enter> ................ search 'ident*' press Enter
2w .......................... jump 2 words until number one  (before dot)
Ctrl-a ...................... increases the number
2w .......................... jump to number after dot
1500 Ctrl-a ................. perform increases 1500 times
q ........................... stop record to macro 'a'

如果您现在有 300 行带有此模式的行,

300@a

Using macro

qa .......................... start record macro 'a'
/iden<Enter> ................ search 'ident*' press Enter
2w .......................... jump 2 words until number one  (before dot)
Ctrl-a ...................... increases the number
2w .......................... jump to number after dot
1500 Ctrl-a ................. perform increases 1500 times
q ........................... stop record to macro 'a'

if you have 300 lines with this pattern just now making

300@a
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文