string - 使用 sed 和 regexp 增加文件中的数字
问题描述
我想使用以下模式将文件中的所有数字增加 1000:
'... comment_count') VALUES ( 15132, ...
'... comment_count') VALUES ( 15133, ...
'... comment_count') VALUES ( 16134, ...
.
.
我希望更改后的输出是这样的:
'... comment_count') VALUES ( 16132, ...
'... comment_count') VALUES ( 16133, ...
'... comment_count') VALUES ( 17134, ...
.
.
我尝试了这样的事情但不起作用:
sed -r 's/`comment_count`\) VALUES \( (\d+)/echo "\1\1$((\1+1000))\"/ge' test.txt
解决方案
sed
不能在替换中进行数学或字符串操作。
您可以使用此perl
解决方案:
perl -pe "s~(comment_count'\)\h+VALUES\h+\(\h+)(\d+)~\$1.(\$2+1000)~e" file
'... comment_count') VALUES ( 16132, ...
'... comment_count') VALUES ( 16133, ...
'... comment_count') VALUES ( 17134, ...
推荐阅读
- javascript - 创建 HTML 元素的 Javascript 类
- java - 带有递归函数的Java返回
- javascript - AJAX 工具包 ConfirmButtonExtender 未打开确认窗口
- python - 计算 n 个数字的最大值、最小值和平均值
- vb.net - 如何从谷歌地图位置链接获取经度和纬度?
- python - rxpy 高效地组合 observables
- node.js - 热重载 HMR Webpack 在 Docker 容器 WSL2 上不起作用
- discord.js - 运行 eval 命令时无法发送空消息错误
- python - tweepy.cursor 不可调用pylint(不可调用)
- python - 如何根据多个条件更改 Pandas Dataframe 中的一个单元格?