我有一个形式的文本文件:
a = 1 #b = [2,3] c = 4 d = [5,6] e = [7,8]我想用一个数字替换括号(和括号)内的模式,但忽略注释中的匹配,最好使用sed。
对于只有一个匹配行的文件,我已经使用过
sed -i "/^#/!s/\[.*\]/9/" myfile如果还有更多,如何修改它以仅替换第一场比赛?
I have a text file of the form:
a = 1 #b = [2,3] c = 4 d = [5,6] e = [7,8]I want to replace the pattern inside the brackets (and the brackets) with a number, but ignore matches in the comments, preferably using sed.
For files with exactly one matching line, I've used
sed -i "/^#/!s/\[.*\]/9/" myfileHow can this be modified to replace only the first match if there are more?
最满意答案
这是正确的,因为只是改变了第一次发生。
awk '!end && /^[^#]+ = \[/ {$3="9"; end=1}1' myfile
如果没有结束标志,并且行不是从#开始并匹配到= [ ,则更改第三列并设置标志以防止在下次出现时更改。a = 1
#b = [2,3]
c = 4
d = 9
e = [7,8] <---这不会随意改变
This is the correct, because changing just the first occurence.
awk '!end && /^[^#]+ = \[/ {$3="9"; end=1}1' myfile
if there is not end flag and line is not beggining from # and match to = [, then change the third column and set the flag to prevent changing in next occurences.a = 1
#b = [2,3]
c = 4
d = 9
e = [7,8] <--- this is not changed as you want
更多推荐
发布评论