Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2004
    Posts
    11

    Unanswered: insert to an empty file

    Hi,
    I need to insert something into an empty file

    touch newfile
    sed -f insertcode newfile


    where insertcode is
    1i\
    first line;


    But the file still remains empty.Could someone tell me what's wrong out here?


    Also can I write a variable in insertcode? eg.

    define VARIABLE
    touch newfile
    sed -f insertcode newfile

    where insertcode is
    1i\
    first line with VARIABLE;


    Thanks

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    sed doesn't modify the input file, it writes the result to stdout.
    You must redirect the output of sed into a file :

    sed -f insertcode newfile > new_newfile


    If you just want to insert datas in an empty file don't use sed.

    Code:
    Variable="contents of Variable"
    echo "First line;"    > newfile
    echo "Second line;"   >> newfile
    echo "Last line with $Variable" >> newfile
    > newfile : reset newfile and redirect stdout to newfile
    >> newfile : redirect stdout to newfile in append mode

    another method:
    Code:
    Variable="contents of Variable"
    cat <<EOD_CAT >newfile
    First line;
    Second line;
    Last line with $Variable
    EOD_CAT
    Jean-Pierre.

Posting Permissions

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