Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 在文件中查找特定文本并与其他文本一起换行_Regex_Linux_Sed_Awk - Fatal编程技术网

Regex 在文件中查找特定文本并与其他文本一起换行

Regex 在文件中查找特定文本并与其他文本一起换行,regex,linux,sed,awk,Regex,Linux,Sed,Awk,我正在寻找最好的方法来查找文件中的特定文本,并从Linux命令行将其他文本环绕在其周围 因此,例如,该文件可能包含以下内容: This is a test. anchor-start text to wrap will is in here anchor-end 因此,在上面的示例中,我希望找到介于锚定开始和锚定结束之间的文本,然后将该文本换行,以以下方式结束: This is a test. anchor-start wrapping-start text to wrap will is i

我正在寻找最好的方法来查找文件中的特定文本,并从Linux命令行将其他文本环绕在其周围

因此,例如,该文件可能包含以下内容:

This
is
a
test.
anchor-start
text to wrap will is in here
anchor-end
因此,在上面的示例中,我希望找到介于
锚定开始
锚定结束
之间的文本,然后将该文本换行,以以下方式结束:

This
is
a
test.
anchor-start
wrapping-start
text to wrap will is in here
wrapping-end
anchor-end

我还应该注意到,我正在寻找一种解决方案,如果
锚定开始
之后紧接着
包装开始
(即包装已经发生),则不应重复该解决方案。

这可能适用于您:

sed '/anchor-start/,/anchor-end/{/anchor-start/{h;d};H;/anchor-end/{x;/wrapping-start/b;s/\n.*\n/\nwrapping-start&wrapping-end\n/p};d}' file
This
is
a
test.
anchor-start
wrapping-start
text to wrap will is in here
wrapping-end
anchor-end

查看sed并将
锚定起点
替换为
锚定起点\n锚定起点
并将
锚定终点
替换为
锚定终点\n锚定终点
要排除双重环绕,可以使用负面环视。
pearl.319> cat temp.pl
This 
is a 
test. 
anchor-start
text to wrap will 
is in here 
anchor-end
pearl.320> perl -p -e 's/anchor-start/anchor-start\nwrap-start/g;s/anchor-end/wrap-end\nanchor-end/g' temp.pl
This 
is a 
test. 
anchor-start
wrap-start
text to wrap will 
is in here 
wrap-end
anchor-end