Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have several files which contain some PHP includes and I want to substitute them with the file contents. The file looks like

more content

So after foo should be the content of file1.php and after baz should be first content of file2.php followed by file3.php. I want also remove the <?php and ?> tags in both cases. What is a good way to resolve this?

Assume the following file contents:

  1. file1.php: is empty
  2. file2.php: <p>Hello
  3. file3.php: World</p>

So the resulting file should look like:

more content

The word foo, bar etc. are just expletives within the real file there could be any text.

share|improve this question
You want to remove <?php to ?> or what? – Rahul Patil Dec 19 '12 at 9:19
I restated my question a bit. I want to get the file contents plus remove the PHP tags. – qbi Dec 19 '12 at 9:28
have you checked my bash script ? – Rahul Patil Dec 19 '12 at 10:20
ohh, sorry , i haven't see your updated question, let me try again – Rahul Patil Dec 19 '12 at 14:03
i have updated my script , so please check and let me know – Rahul Patil Dec 19 '12 at 14:22

5 Answers 5

up vote 3 down vote accepted

These are PHP substitutions anyway, so just run it through php:

$ php file-main.php 
more content

$ php file-main.php > file-main-new.php
share|improve this answer
what about include file contains echo "hello"; it will displayed only hello, so it useful in new.php ? – Rahul Patil Dec 19 '12 at 15:33
It works for the OP's sample case, but yes - actual PHP code would be executed rather than copied – Useless Dec 19 '12 at 15:35
while read line

        if [[ "${line}" == "<?php" ]] || [[ "${line}" == "?>" ]]; then
        elif [[ "${line}" =~ "include" ]]; then
                while read include_line
                echo $include_line  | grep -Ev '<\?php|\?>' >> newfile.php
                done <${file:9:9}
       # cat file$k.php >> newfile.php
        echo $line >> newfile.php

done </tmp/inputfile
share|improve this answer
I think, you somehow misunderstood me. I don't want to skip the include, but I want to insert the content of the file which is in the include-statement. – qbi Dec 19 '12 at 13:55
have you tried that ? it's just skipping <?php ?> before foo as you want. – Rahul Patil Dec 19 '12 at 13:57
Please run that script with test files and see the result. – Rahul Patil Dec 19 '12 at 13:59
It skips <?php and ?>, but does not include any file contents. – qbi Dec 19 '12 at 14:00
now it will include that file as you want – Rahul Patil Dec 19 '12 at 14:24

awk wrapped:

awk '
    /</ || /\?>/ {next;} 
    /include/ {
        if (match($0,"\".*php")){
            f = substr($0,RSTART+1,RLENGTH-1);
            while ( getline < f ){

Can embed in shell, mend your one-liner, or save into an awk script which is recommended...

share|improve this answer
it's giving error : awk: line 2: regular expression compile failed (missing operand) – Rahul Patil Dec 19 '12 at 10:38
My Bad. I forgot to escape the "?" in regex, which is not needed in gawk but needed on proper AWK like mawk and others. Corrected. – MeaCulpa Dec 20 '12 at 1:16

Depending on how structured your data is, you could do it like this with sed:

<infile.php sed -n 's:include("\([^"]\+\)");:/&/ { r \1\nd }:p' | 
  sed -f - -e '/<\?php\|\?>/d' infile.php

The first invocation of sed writes a sed script with lines like:

/include("file1.php");/ { r file1.php
d }

Which replace matched lines with the file content they mention. The last sed then executes this script along with /<\?php\|\?>/d, which deletes lines with <?php and ?>.

share|improve this answer

There would be better awk, sed, perl solutions to this. But I am giving a simple editing tip of vim that can be used here, which is used to insert the contents of a files into other.

  1. Delete the <?php and ?> tags.
  2. Delete the include line, and insert the respective contents of the file using :r file1.php. This will insert the contents of the file file1.php at the next line from the cursor.
  3. Repeat the steps for other such include statements.


$ cat file1.txt
This is a content in file1.

The screenshots show the changes


The contents of file1.txt are placed in the mainFile.txt

You can check more about :r in the vim help or this reference link.

share|improve this answer
This seems too much work if you have hundreds of files. However you can record a sequence of keystrokes and replay it, like: / *include("<RET>f"lgfvaG:b#<RET>kpjdd. Now one has to remove the PHP tags and is done. :-) – qbi Dec 19 '12 at 23:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.