0

我想处理内容上的 PHP 文件,但我只对< ?php ... ?>之间的部分感兴趣。其余部分应删除并保存到新文件中。下面我已经根据此页面多行 sed search的代码编写了部分代码。但它只给出了第一部分。

start="<?php"
end="?>"
temp=$(sed -n '1h;1!H;${;g;s/.*'"$start"'//pI;d;}' <<< echo $filename)
output=$(sed -n '1h;1!H;${;g;s/'"$end"'.*//p;d;}' <<< "$temp")

有人有解决方案吗?awk、grep 也可以。谢谢

4

1 回答 1

2

这个单线可能适合您:

 awk '/<?php/{f=1} /?>/&&f{print;f=0} f'  file

如果您不希望<?phpand?>在输出中:

 awk '/<?php/{f=1;next;} /?>/&&f{f=0} f' file

测试:(我希望输出是您要查找的内容。要保存到新文件,您可以将输出重定向到新文件)

kent$  cat test.txt 
<?php
all
text

here
should
be
saved

even if 

empty

lines

?>


foo

bar

should be removed

kent$  awk '/<?php/{f=1} /?>/&&f{print;f=0} f' test.txt                                                                                                                     
<?php
all
text

here
should
be
saved

even if 

empty

lines

?>
于 2012-12-19T01:03:16.090 回答