Sed 塞德:还记得第二个表达式中的捕获吗

Sed 塞德:还记得第二个表达式中的捕获吗,sed,gnu-sed,Sed,Gnu Sed,我试图找到捕获模式的出现,并用捕获模式预挂起下一行 例如: ... [line 10] #---------- SOLID: tank_phys.0 [line 11] Shape { ... [line 22] #---------- SOLID: head_phys.0 [line 23] Shape { ... 预期产出: ... [line 10] #---------- SOLID: tank_phys.0 [line 11] DEF tank Shape { ...

我试图找到捕获模式的出现,并用捕获模式预挂起下一行

例如:

...
[line 10] #---------- SOLID: tank_phys.0
[line 11]   Shape {
...
[line 22] #---------- SOLID: head_phys.0
[line 23]   Shape {  
...
预期产出:

...
[line 10] #---------- SOLID: tank_phys.0
[line 11]   DEF tank Shape {
...
[line 22] #---------- SOLID: head_phys.0
[line 23]   DEF head Shape {   
...
以下是我所拥有的:

sed -rn '/#---------- SOLID: (.*)_phys.0/{n ; s/Shape/DEF <PreviousCapture> Shape/p;}' g4_00.wrl
sed-rn'/#------------固体:(.*)_phys.0/{n;s/Shape/DEF Shape/p;}'g4_00.wrl
如何将
形状{
替换为
DEF油箱形状{

谢谢


GT

遵循简单的
awk
同样可以帮助您

awk '/#---------- SOLID/{print;sub(/_.*/,"",$NF);val=$NF;getline;sub("Shape {","DEF " val " &")} 1'  Input_file
输出如下

[line 10] #---------- SOLID: tank_phys.0
[line 11]   DEF tank Shape {
...
[line 22] #---------- SOLID: head_phys.0
[line 23]   DEF head Shape {

使用纯
sed
解决方案:

输入:

$ cat file
#---------- SOLID: tank_phys.0
  Shape {
abcdef
1234
#---------- SOLID: head_phys.0
  Shape { 
12345
gdfg
$ sed -rn '/#---------- SOLID: (.*)_phys.0/{p;s/#---------- SOLID: (.*)_phys.0/DEF \1/;N;s/\n//;s/ {2,}/ /;s/^/  /p;b};/#---------- SOLID: (.*)_phys.0/!p' file
#---------- SOLID: tank_phys.0
  DEF tank Shape {
abcdef
1234
#---------- SOLID: head_phys.0
  DEF head Shape { 
12345
gdfg
/#---------- SOLID: (.*)_phys.0/{ #this block will be executed on each line respecting the regex /#---------- SOLID: (.*)_phys.0/
p; #print the line
s/#---------- SOLID: (.*)_phys.0/DEF \1/; #replace the line content using backreference to form DEF ...
N;#append next line Shape { to the pattern buffer
s/\n//;s/ {2,}/ /;s/^/  /p; #remove the new line, add/remove some spaces
b}; #jump to the end of the statements
/#---------- SOLID: (.*)_phys.0/!p #lines that does not respect the regex will just be printed
命令:

$ cat file
#---------- SOLID: tank_phys.0
  Shape {
abcdef
1234
#---------- SOLID: head_phys.0
  Shape { 
12345
gdfg
$ sed -rn '/#---------- SOLID: (.*)_phys.0/{p;s/#---------- SOLID: (.*)_phys.0/DEF \1/;N;s/\n//;s/ {2,}/ /;s/^/  /p;b};/#---------- SOLID: (.*)_phys.0/!p' file
#---------- SOLID: tank_phys.0
  DEF tank Shape {
abcdef
1234
#---------- SOLID: head_phys.0
  DEF head Shape { 
12345
gdfg
/#---------- SOLID: (.*)_phys.0/{ #this block will be executed on each line respecting the regex /#---------- SOLID: (.*)_phys.0/
p; #print the line
s/#---------- SOLID: (.*)_phys.0/DEF \1/; #replace the line content using backreference to form DEF ...
N;#append next line Shape { to the pattern buffer
s/\n//;s/ {2,}/ /;s/^/  /p; #remove the new line, add/remove some spaces
b}; #jump to the end of the statements
/#---------- SOLID: (.*)_phys.0/!p #lines that does not respect the regex will just be printed
输出:

$ cat file
#---------- SOLID: tank_phys.0
  Shape {
abcdef
1234
#---------- SOLID: head_phys.0
  Shape { 
12345
gdfg
$ sed -rn '/#---------- SOLID: (.*)_phys.0/{p;s/#---------- SOLID: (.*)_phys.0/DEF \1/;N;s/\n//;s/ {2,}/ /;s/^/  /p;b};/#---------- SOLID: (.*)_phys.0/!p' file
#---------- SOLID: tank_phys.0
  DEF tank Shape {
abcdef
1234
#---------- SOLID: head_phys.0
  DEF head Shape { 
12345
gdfg
/#---------- SOLID: (.*)_phys.0/{ #this block will be executed on each line respecting the regex /#---------- SOLID: (.*)_phys.0/
p; #print the line
s/#---------- SOLID: (.*)_phys.0/DEF \1/; #replace the line content using backreference to form DEF ...
N;#append next line Shape { to the pattern buffer
s/\n//;s/ {2,}/ /;s/^/  /p; #remove the new line, add/remove some spaces
b}; #jump to the end of the statements
/#---------- SOLID: (.*)_phys.0/!p #lines that does not respect the regex will just be printed
解释:

$ cat file
#---------- SOLID: tank_phys.0
  Shape {
abcdef
1234
#---------- SOLID: head_phys.0
  Shape { 
12345
gdfg
$ sed -rn '/#---------- SOLID: (.*)_phys.0/{p;s/#---------- SOLID: (.*)_phys.0/DEF \1/;N;s/\n//;s/ {2,}/ /;s/^/  /p;b};/#---------- SOLID: (.*)_phys.0/!p' file
#---------- SOLID: tank_phys.0
  DEF tank Shape {
abcdef
1234
#---------- SOLID: head_phys.0
  DEF head Shape { 
12345
gdfg
/#---------- SOLID: (.*)_phys.0/{ #this block will be executed on each line respecting the regex /#---------- SOLID: (.*)_phys.0/
p; #print the line
s/#---------- SOLID: (.*)_phys.0/DEF \1/; #replace the line content using backreference to form DEF ...
N;#append next line Shape { to the pattern buffer
s/\n//;s/ {2,}/ /;s/^/  /p; #remove the new line, add/remove some spaces
b}; #jump to the end of the statements
/#---------- SOLID: (.*)_phys.0/!p #lines that does not respect the regex will just be printed
你可以试试这个

sed -E '
  /SOLID/!b
  N
  s/(^.*SOLID: )([^_]*)(.*\n)([[:blank:]]*)(.*$)/\1\2\3\4DEF \2 \5/
' infile

将第一个
sed
的结果存储在一个变量中,然后在第二个
sed
中使用该变量?请在您的帖子中的代码标签中添加更多示例,这不清楚。如果我想一次性处理整个文件,这将不起作用。该文件包含我显示的模式的多个实例。谢谢。如果您能告诉我您是如何处理的,也许可以我们在想什么?你能检查一下我的
sed
解决方案吗?效果很好,现在让我觉得我对sed一无所知。thanksI更新了输入,以明确行数不总是相同的,因此此awk解决方案不起作用,thanks@user1301295,您的输出在您的帖子中看起来不一致,显示了为什么
{
不是第一次出现的
形状
?如果是打字错误,那么你会尝试我的代码。我刚刚测试过,它工作正常,请告诉我。编辑完成,谢谢。你能解释更多代码吗?我只是在学习sed,awk对我来说是新的me@user1301295,请务必根据您的需要编辑代码,并将添加说明太快了,干杯:)