使用sed和regexp增加文件中的数字 [英] Increase numbers in file using sed and regexp
问题描述
我想按照以下模式将文件中的所有数字增加1000:
I want to increase all numbers in file by 1000 with follow pattern:
'... comment_count') VALUES ( 15132, ...
'... comment_count') VALUES ( 15133, ...
'... comment_count') VALUES ( 16134, ...
.
.
我希望更改输出后像这样:
I want after change output be like this:
'... comment_count') VALUES ( 16132, ...
'... comment_count') VALUES ( 16133, ...
'... comment_count') VALUES ( 17134, ...
.
.
我尝试了类似的方法,但是没有用:
I tried something like this but not work:
sed -r 's/`comment_count`\) VALUES \( (\d+)/echo "\1\1$((\1+1000))\"/ge' test.txt
推荐答案
答案假定所有行都具有OP中显示的格式.
The answer assumes all lines have the format shown in the OP.
以下与sed -E -f plus1000 inputfile
一起运行的sed
脚本(-E
将使用(
,{
等,而不是\(
,\{
等)执行工作:
The following sed
script, run with sed -E -f plus1000 inputfile
(-E
is to use (
, {
, and the likes, instead of \(
, \{
, and the others), does the job:
# this file is named plus1000
s/([^0-9][0-9]*)([0-9])([0-9]{3})/\1\n\2\n\3/
:a
h
s/.*\n(.)\n.*/\1/
y/0123456789/1234567890/
G
/^0/{
s/0\n(.*)(.)\n.\n(.*)/\1\n\2\n0\3/
ta
}
s/(.)\n(.*)\n.\n(.*)/\2\1\3/
说明:
-
顶部
s/…/…/
命令在两个换行符\n
之间将第四个有效数字括起来;
the top
s/…/…/
command encloses the fourth significant digint in between two newlines\n
;
- 模式空间的内容将匹配
^.*\n[0-9]\n.*$
- 容纳空间的内容无关紧要(我们不在乎内容是什么)
:a
标签标记了 do-while 循环开始的行;
the :a
label marks the line where a do-while loop begins;
- 不更改图案和保留空格
h
命令复制模式空间的内容(当前行已变为当前行),覆盖其中的所有内容;
the h
command copies the content of the pattern space (the current line as it has become), overwriting whatever was in it;
- 图案空间不变
- 保留空间等于图案空间
这另一个s/…/…/
命令可删除突出显示"数字周围的所有内容.在步骤1;
this other s/…/…/
command removes whatever surrounds the digit that was "highlighted" at step 1;
- 模式空间仅包含一位数字,即其格式为
^[0-9]$
- 容纳空间不变
y
命令转换其后继字符中的每个数字,但9变为0,这意味着左边的下一个数字也应增加1;
the y
command transform each digit in its successor, except for 9 which becomes 0, thus implying that the following digit on the left should be increased by 1 too;
- 模式空间仅包含一个数字,该数字相对于上一步增加了1
- 容纳空间不变
G
命令将保留空间的内容追加到模式空间
the G
command appends the content of the hold space to the pattern space
- 容纳空间不变
- 模式空间是第5步中刚刚增加的数字,后跟换行符
\n
,然后是保留空间的内容
- the hold space is unchanged
- the pattern space is the digit just increased in step 5, followed by a newline
\n
, followed by the content of the hold space
/^0/
将在{
和}
中分组的命令的作用仅限制为具有前导0
(以前是9
)的模式空间
/^0/
limits the action of the commands grouped in {
and }
only to pattern space that has a leading 0
(that previously was a 9
)
7.1.第三个s/…/…/
命令删除在第6步中添加的换行符,并将其他两个换行符移至下一位(向左)数字
7.1. the third s/…/…/
command deletes the newline added in step 6, and moves the other two newlines around the next (to the left) digit
7.2. t
命令在测试中是否确实发生了先前的替换,如果是,则将控制转移到步骤2标记的行(实际上是转移到其后的行,因为:a
不执行任何操作)
7.2. the t
command in tests if the previous substitution actually happened and, if so, it transfer control to the line labelled at step 2 (actually to the line after it, as :a
performs no action)
如果到达此处,我们在9以外的数字上加了1,因此不需要进一步的处理,因此第四个和最后一个s/…/…/
命令对各部分重新排序并删除换行符.
if we reach here, we have added 1 to a digit other than 9, so further processing is not needed, so the fourth and last s/…/…/
command reorders the pieces and removes the newlines.
这篇关于使用sed和regexp增加文件中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!