concatenating selected lines of multiple files

Hi,

I would like a shell script that reads all files in a directory and concatenate them. It is not a simple concatenation. The first few lines of the files should not be included. The lines to be included are the lines from where 'START HERE' appears up to the end of the file. For example, I have 3 files:

File 1:

    abc
    def
    START HERE
    line1
    line2

File 2:

    xyz
    efg
    START HERE
    line111
    line222

File 3:

    jkl
    mno
    START HERE
    line11
    line22

The resulting file should be:

    START HERE
    line1
    line2
    START HERE
    line111
    line222
    START HERE
    line11
    line22

Thanks in advance...

Try this:

awk 'FNR==1{f=0}/START HERE/{f=1}f' file1 file2 file3

Thanks Franklin, but the number of files and the filenames may vary. It should be dynamic that it will process all files in the directory... Please modify..

It's up to you to make it more dynamic...
For example:

awk 'FNR==1{f=0}/START HERE/{f=1}f' *
  • will expand to all files in the current directory

Thanks much! That works! I got a new requirement today in addition to that. I have to attach a header to it. I have a file header whose contents (which may vary) I need to prefix to the concatenation. For example:

FileHeader:

FILES AS OF TODAY:
HEADER LINE1
HEADER LINE2

And the result should be:

FILES AS OF TODAY:
HEADER LINE1
HEADER LINE2
    START HERE
    line1
    line2
    START HERE
    line111
    line222
    START HERE
    line11
    line22

I can only think of doing this:

awk 'FNR==1{f=0}/START HERE/{f=1}f' * > concat.txt
cat concat.txt >> FileHeader

Any better way to do this????