Bash / SED:贪婪与非贪婪

问题描述 投票:0回答:1

嘿,我需要正则表达式方面的帮助

sed -i "" 's/\(.*\)\( @[^@]*\)/path:*\1*\2/' "$OUTPUT_LOCATION"

这会变成这样

/Tests/UnitTests/ViewControllers/Test.UnitTest.swift @team1
/Tests/UnitTests/Auth.UnitTest.swift @team2
/Modules/Stuff/ @team1 @team2

进入

path:*/Tests/UnitTests/ViewControllers/Test.UnitTest.swift* @team1
path:*/Tests/UnitTests/Auth.UnitTest.swift* @team1
path:*/Modules/Stuff/ @team1* @team2

但是如果您注意到最后一行

*
在负责
/Modules/Stuff/
的两个团队之前位于错误的位置?如何修改正则表达式来查找第一个@?

regex sed command-line text-processing
1个回答
0
投票

我建议您修改第一个正则表达式以仅匹配您感兴趣的内容

[^ ]*
。 它通常更简单、更健壮:

$ sed 's/[^ ]*/path:*&*/' input.txt
path:*/Tests/UnitTests/ViewControllers/Test.UnitTest.swift* @team1
path:*/Tests/UnitTests/Auth.UnitTest.swift* @team2
path:*/Modules/Stuff/* @team1 @team2
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.