假设我们有这句话:
输入:
This is some string (234) and so on
输出:
This is some string (page 234) and so on
问题是如何在找到数字时添加“页面”,数字带括号。
答案 0 :(得分:2)
您可以使用以下sed
:
sed 's/(\([[:space:]]*[[:digit:]]\+[[:space:]]*\))/(page \1)/g' file
$ cat file
This (is) some string (234) and so on
(This is some string ( 1324) and so on
This is some string (text) and so on
$ sed 's/(\([[:space:]]*[[:digit:]]\+[[:space:]]*\))/(page \1)/g' file
This (is) some string (page 234) and so on
(This is some string (page 1324) and so on
This is some string (text) and so on
答案 1 :(得分:1)
这可以通过很多方式解决,并且非常简单,您应该使用谷歌找到至少一个解决方案。这里有一些非常简单。
sed 's/(/(page /g' file
This is some string (page 234) and so on
要测试数字,你可以这样做:
awk -F"[()]" '{for (i=1;i<=NF;i++) {if ($(i%2)~"^[0-9]+$") $i="page "$i;printf "%s%s%s",(i%2==1 && i!=1?")":""),$i,(i%2 && i!=NF?"(":"")}print ""}' file
This is some string (page 234) and so on
以下是基于jaypals正则表达式的gnu awk
。
awk '{print gensub(/\(([[:space:]]*[[:digit:]]+[[:space:]]*)\)/,"(page \\1)","")}' file
This is some string (page 234) and so on (not this)45(nor this) more