Find the answer to your Linux question:
Results 1 to 6 of 6
Hi Everybody, sed '/<pattern>/r <file1>' <file2> This adds the contents of file1 in file2 after pattern, But I want to insert some data from a file before a pattern . ...
  1. #1
    Linux Newbie
    Join Date
    Jul 2004
    Posts
    143

    reg sed

    Hi Everybody,

    sed '/<pattern>/r <file1>' <file2>
    This adds the contents of file1 in file2 after pattern,

    But I want to insert some data from a file before a pattern. How to do that by using sed.

    Please suggest me...

    Thanks Inadvance
    Mummaneni.

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Can you include a sample of the files and what you wish to accomplish?

    Regards

  3. #3
    Linux Newbie
    Join Date
    Jul 2004
    Posts
    143
    Hi,

    lets assume the contents of the files:
    file1 contains
    one
    two
    three
    four
    and file2 contains
    seven
    eight
    nine

    by using sed, we can insert the data of a file after a pattern

    sed '/three/r file1' file2
    now the O/P will be:
    one
    two
    three
    seven
    eight
    nine
    four

    But I want to add contents of file2 before three of file1
    for ex:
    one
    two
    seven
    eight
    nine
    three
    four

    This is my requirement.. please help me out

    Thanks & Regards,
    Mummaneni.

  4. #4
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    I think I see what he wants.

    Say file1 contains:
    aaa
    bbb
    ccc
    ddd
    eee

    file2 contains:
    111
    222
    333
    444
    555

    If /<pattern>/ is /333/ he wants the result to be:
    111
    222
    aaa
    bbb
    ccc
    ddd
    eee
    333
    444
    555

    I came up with not one sed command but this:
    sed '/333/,$d' file2; cat file1; sed -n '/333/,$p' file2

    I know it's a kludge but it works.

  5. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    If the file is not too long, you can script ed. It can do arithmetic on line addresses:
    Code:
    #!/bin/sh
    
    # @(#) s2       Demonstrate ed insert.
    
    set -o nounset
    echo " sh version: $BASH_VERSION"
    
    cat >data1 <<EOF
    one
    two
    three
    four
    five
    EOF
    
    cat >data2 <<EOF
    3.1
    3.2
    EOF
    
    ed data1 <<EOF
    /four/-1
    . r data2
    w
    q
    EOF
    
    cat -n data1
    
    # See man ed for details.
    
    exit 0
    producing:
    Code:
    % ./s2
     sh version: 2.05b.0(1)-release
    24
    three
    8
    32
         1  one
         2  two
         3  three
         4  3.1
         5  3.2
         6  four
         7  five
    cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by vsemaska View Post
    I think I see what he wants.

    Say file1 contains:
    aaa
    bbb
    ccc
    ddd
    eee

    file2 contains:
    111
    222
    333
    444
    555

    If /<pattern>/ is /333/ he wants the result to be:
    111
    222
    aaa
    bbb
    ccc
    ddd
    eee
    333
    444
    555

    I came up with not one sed command but this:
    sed '/333/,$d' file2; cat file1; sed -n '/333/,$p' file2

    I know it's a kludge but it works.
    This should work too:

    Code:
    sed '/333/{x;p;x;}' file2|sed -e '/^$/r file1' -e '/^$/d'
    Regards

Posting Permissions

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