sed:记住在第二个表达式中捕获
sed: remember capture in second expression
我正在尝试查找捕获模式的出现并在下一行添加捕获模式。
例如:
...
[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
如何将 Shape {
替换为 DEF tank Shape {
?
谢谢
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 /;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 /; #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
sed -E '
/SOLID/!b
N
s/(^.*SOLID: )([^_]*)(.*\n)([[:blank:]]*)(.*$)/DEF /
' infile
我正在尝试查找捕获模式的出现并在下一行添加捕获模式。
例如:
...
[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
如何将 Shape {
替换为 DEF tank Shape {
?
谢谢
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 /;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 /; #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
sed -E '
/SOLID/!b
N
s/(^.*SOLID: )([^_]*)(.*\n)([[:blank:]]*)(.*$)/DEF /
' infile