Bash sed 非贪婪匹配

use*_*961 2 bash sed non-greedy

这是我的文字:

1a.begin /path/1a.file
2bx.begin2 /path/my/2bx.file2
Run Code Online (Sandbox Code Playgroud)

预期输出是

begin /path/1a.file
begin2 /path/my/2bx.file2
Run Code Online (Sandbox Code Playgroud)

这里我想通过 sed 使用非贪婪匹配来做到这一点。(sed默认匹配是贪婪的,所有的1a.和2bx.都会被删除)

因此我尝试了命令:

echo -e "1a.begin /path/1a.file\n2bx.begin2 /path/my/2bx.file2"|sed 's/$.*[^\.]\.//g'
Run Code Online (Sandbox Code Playgroud)

我使用 来$.*匹配从行首开始的所有字符串。我曾经[^\.]防止贪婪匹配.一行中的所有内容(请参阅https://www.unix.com/shell-programming-and-scripting/133641-non-greedy-sed.html中的类似方法)但它没有改变文本。

那么我的脚本哪里错了?

Hat*_*ess 7

  • 您的行首锚点$错误,您应该使用^
  • 您使用贪婪匹配.*直到最后一个周期.

使用sed

$ sed 's/^[^.]*\.//' input_file
begin /path/1a.file
begin2 /path/my/2bx.file2
Run Code Online (Sandbox Code Playgroud)