Inset one file inside another file after matching line

  • Follow


Hi all,

I would like to know how to include a file in to another file at a
specified location. The condition is that AWK should search for the
matching pattern first and insert the contents of the file after the
match.

Ex: cat file1.dat
line1
line2
.......
line10

cat file2.dat
line21
line22
........
line30

I would like to get an output (after the matching expression line2)
like,
line1
line2
line21
line22
........
line30
line3
line4
.......
line10

with thanks and regards
arun
0
Reply arun 2/18/2011 4:37:32 PM

On 2/18/2011 10:37 AM, arun wrote:
>
> Hi all,
>
> I would like to know how to include a file in to another file at a
> specified location. The condition is that AWK should search for the
> matching pattern first and insert the contents of the file after the
> match.
>
> Ex: cat file1.dat
> line1
> line2
> ......
> line10
>
> cat file2.dat
> line21
> line22
> .......
> line30
>
> I would like to get an output (after the matching expression line2)
> like,
> line1
> line2
> line21
> line22
> .......
> line30
> line3
> line4
> ......
> line10
>
> with thanks and regards
> arun

This isn't exactly what you want but it's something I had lying around as a rare 
example of when using getline is appropriate and it should be easy for you to 
modify.

This script will not only expand all the lines that say "include subfile", but
by writing the result to a tmp file, resetting ARGV[1] (the highest level input
file) and not resetting ARGV[2] (the tmp file), it then lets awk do any normal
record parsing on the result of the expansion since that's now stored in the
tmp file. If you don't need that, just do the "print" to stdout and remove any
other references to a tmp file or ARGV[2].

awk 'function read(file) {
        while ( (getline < file) > 0) {
            if ($1 == "include") {
                 read($2)
            } else {
                 print > ARGV[2]
            }
        }
        close(file)
    }
    BEGIN{
       read(ARGV[1])
       ARGV[1]=""
       close(ARGV[2])
    }1' a.txt tmp

The result of running the above given these 3 files in the current directory:

       a.txt             b.txt              c.txt
       -----             -----              -----
       1                 3                  5
       2                 4                  6
       include b.txt     include c.txt
       9                 7
       10                8

would be to print the numbers 1 through 10 and save them in a file named "tmp".

Regards,

	Ed.
0
Reply Ed 2/18/2011 5:09:18 PM


On 18.02.2011 17:37, arun wrote:
> 
> Hi all,
> 
> I would like to know how to include a file in to another file at a
> specified location. The condition is that AWK should search for the
> matching pattern first and insert the contents of the file after the
> match.

I can think of various (standard awk) approaches, but probably the
most legible one (i.e. simple conditions in the code, no imperative
programming, like getline, etc.) would be using gawk, e.g.

  gawk -v pat="match" '

    ARGIND==1 && !fnr
    ARGIND==1 && ($0 ~ pat) { fnr=FNR; nextfile }
    ARGIND==2
    ARGIND==3 && FNR>fnr

  ' file1.dat file2.dat file1.dat


Please consider also that non-awk solutions may yet be much simpler
and preferable for your task, if applicable in your context, e.g.

  sed -e '/match/r file2.dat' file1.dat


Janis

> 
> Ex: cat file1.dat
> line1
> line2
> ......
> line10
> 
> cat file2.dat
> line21
> line22
> .......
> line30
> 
> I would like to get an output (after the matching expression line2)
> like,
> line1
> line2
> line21
> line22
> .......
> line30
> line3
> line4
> ......
> line10
> 
> with thanks and regards
> arun

0
Reply Janis 2/19/2011 12:09:27 PM

In article 
<6dc8a91d-b3d8-408f-87d3-21b07d095db7@a8g2000pri.googlegroups.com>
,
 arun <arunkumar.cr@gmail.com> wrote:

> Hi all,
> 
> I would like to know how to include a file in to another file at a
> specified location. The condition is that AWK should search for the
> matching pattern first and insert the contents of the file after the
> match.
> 
> Ex: cat file1.dat
> line1
> line2
> ......
> line10
> 
> cat file2.dat
> line21
> line22
> .......
> line30
> 
> I would like to get an output (after the matching expression line2)
> like,
> line1
> line2
> line21
> line22
> .......
> line30
> line3
> line4
> ......
> line10
> 
> with thanks and regards
> arun

awk -v file2="file2.dat" '
    { print }
    found { next }
    /match/ { system("cat " file2); found=1 }
' file1.dat >combined.dat
0
Reply Bob 2/20/2011 12:46:59 AM

3 Replies
973 Views

(page loaded in 0.07 seconds)

Similiar Articles:













7/20/2012 6:06:50 PM


Reply: