jjacobs
jjacobs

Reputation: 57

How to process file content in bash

I want to process PHP files on the content but i'm only interested in the parts between < ?php ... ?>. The rest should be removed and saved to a new file. Below I've already part of the code based on the code from this page multi line sed search. But it only gives the first part in .

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")

Somebody has a solution? awk, grep is also oké. THanks

Upvotes: 0

Views: 100

Answers (1)

Kent
Kent

Reputation: 195209

this one-liner may work for you:

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

if you don't want the <?php and ?> in output:

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

test: (I hope the output is what you are looking for. to save to new file, you could redirect output to a new 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

?>

Upvotes: 2

Related Questions