如何在bash中处理文件内容
我想处理内容上的PHP文件,但我只对如何在bash中处理文件内容,bash,sed,file-processing,Bash,Sed,File Processing,我想处理内容上的PHP文件,但我只对。其余部分应删除并保存到新文件中。下面我已经根据本页的代码编写了部分代码。但它只给出了第一部分 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") st
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")
start=“”
temp=$(sed-n'1h;1!H;${;g;s/*'“$start”//pI;d;}'这一行程序可能适合您:
awk '/<?php/{f=1} /?>/&&f{print;f=0} f' file
测试:(我希望输出是您想要的。要保存到新文件,您可以将输出重定向到新文件)
kent$cat test.txt
福
酒吧
应该删除
kent$awk'/&&f{print;f=0}f'test.txt
您可以添加一些示例输入吗?对于预期输出,您不需要显式打印或下一步,只需将条件按正确的顺序排列:awk'/{f=0}'
和awk'f&/?>/{f=0}f/
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
?>