【发布时间】:2023-03-20 07:41:02
【问题描述】:
修改后的工作解决方案:
sed -r "s/(\.*)\[/\1\r[/g"
更好的解决方案:
sed -r "s/(.*)\[/\1\r[/g"
分解:
s/
(\.*)\[ -String to Capture followed by [
/
\1\r[ -Line to replace with
/g"
I believe for subsequent strings, more of these are made
(\.*) but in the same order they appear from left to right, the variables are referenced.
请尝试将答案保留为有效的 sed 行,并附上替换操作的描述。
我确实想学习如何在 sed 中使用变量。因此,如果您有其他解决方案,我会全力以赴,但我真的很想学习如何在 sed 中操作变量。
我试过了
$1、%1 和 1
作为这些的组合 \$1, \%1 \1 和 /$1 /%1 /1
无济于事。
这是我用空白部分替换匹配部分的起始工作脚本。
sed -e "s/\.*\[//g" testfile.txt
我想要对脚本做的是替换(* 表示任何先前的(非空白)字符串)
*[
with(中间没有空行或制表符)
*
[
所以我想到了类似的东西
C:\temp>sed -e "s/\.*\[/\1/g" testfile.txt
sed: -e expression #1, char 12: invalid reference \1 on `s' command's RHS
【问题讨论】:
-
请尽量将答案保留为工作 sed 行,并附上替换操作的描述。
-
您的输入和预期输出是什么?
-
输出在问题中
-
如果你想使用
\1需要捕获与\(...\)匹配的部分 -
您能提供一个工作示例吗?你在谈论这样的事情吗? stackoverflow.com/questions/16637799/…