Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138

    Unanswered: insert a file in a file

    I have a file1 with following lines :

    aaa
    bbb
    ccc
    ddd

    Now I want to insert file2

    111
    222

    after the line bbb from file1

    that I have following effect in file1:

    aaa
    bbb
    111
    222
    ccc
    ddd


    can anyone help me out by this problem ?

    thanx a lot
    Greetings from germany
    Peter F.

  2. #2
    Join Date
    Jan 2004
    Posts
    18

    Re: insert a file in a file

    Originally posted by fla5do
    I have a file1 with following lines :

    aaa
    bbb
    ccc
    ddd

    Now I want to insert file2

    111
    222

    after the line bbb from file1

    that I have following effect in file1:

    aaa
    bbb
    111
    222
    ccc
    ddd


    can anyone help me out by this problem ?

    thanx a lot

    Hi

    head -n 2 file1 > file3
    cat file2 >> file3
    tail -n 2 file1>> file3

    and then if the file3 is true that you want,
    mv file3 file1

    you should change number of lines parameter (I used 2) head and tail commands, but if you want more functionallity you have to supply more info
    Murat SULUHAN
    Live Free or Die

  3. #3
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    An awk solutiion for you...

    awk '{print}/bbb/{while (getline < "file1" > 0){print}}' file2

    Damian

  4. #4
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Thanxs for your interest to help me,

    some more info !!
    The position of pattern bbb can be in n th line

    I have a file1 with following lines :

    aaa
    n th lines ....
    .....
    .....
    bbb
    ccc
    ddd

    @ Damian.
    I think you are on the right way, but file2 is given out to screen
    and not into file1
    Greetings from germany
    Peter F.

  5. #5
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Damian just invert file1 and file2.
    If you want to replace file1, redirect output to a file, for example :
    Code:
    awk '{print}/bbb/{while (getline < "file2" > 0){print}}' file1 > file1.tmp
    mv file1.tmp file1
    Jean-Pierre.

  6. #6
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    I think you are on the right way, but file2 is given out to screen
    and not into file1
    Apologies Peter, I mixed up your filenames. I also took it as read that you'd know to redirect the output if you wanted it in a file!

    By the way, what would you want to happen if your file contained more than one instance of bbb?

    Damian

  7. #7
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Hi, thanx @ all,
    the string surely exist only for one time in file1. I have to update the etc/profile on 7 hundred machines. After the line "umask 022"(bbb) I have to insert the lines from file2 . File2 contains the commands for a second password question after the normal login is done. There are a lot of other updates needed and I want to do as much as possible automatically by a script.

    Thanx for helping me out.
    With your help I get the solution for my problem.
    Last edited by fla5do; 02-07-04 at 13:43.
    Greetings from germany
    Peter F.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •