涉及算术的 sed 替换可能吗? [英] Sed substitution possible with arithmetic involved?

查看:29
本文介绍了涉及算术的 sed 替换可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要修改的文件包含以下内容:

File I need to modify contains the following:

block: 16, size: 16, start: 8, length: 4

我想要该文件,以便 blocksize 的值除以 2,而 start 的值长度将乘以2.

I'd like the file so that values for block, size will be divided by 2 while values for start, length will be multiplied by 2.

由于我必须对一大堆文件进行这样的修改,我正在考虑使用 Sed 来为我做替换工作.但我不确定在匹配和替换过程中是否允许计算.

Since I have to make such modifications for a whole bunch of files, I am considering using Sed to do the substitution work for me. But I'm not sure calculations are allowed in the matching and substituting process.

推荐答案

我总是尝试解决所有标记为 .但是在这里,使用 awk 完成您想要做的事情会非常容易.(并且在这种情况下使用 sed 太困难了.)所以,这是我使用 awk 的解决方案:

I always try to solve every problem tagged with sed using sed. But here it would be so easy to accomplish what you are trying to do with awk. (And the use of sed in this case is too difficult.) So, here is my solution using awk:

$ echo "block: 16, size: 16, start: 8, length: 4" | awk '{
    printf "%s %d, %s %d, %s %d, %s %d\n", $1, $2/2, $3, $4/2, $5, $6*2, $7, $8*2
}'
block: 8, size: 8, start: 16, length: 8

这篇关于涉及算术的 sed 替换可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆