Find the answer to your Linux question:
Results 1 to 4 of 4
I want to create a script wherein it will put a string somewhere on the text file. But my priority was on top but I hope I can place it ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Mar 2006
    Posts
    101

    put string on top of the text


    I want to create a script wherein it will put a string somewhere on the text file. But my priority was on top but I hope I can place it in any lines if possible. I tried to create a script using redirect ">" and then put it on top of the file.

    The text contains:

    foo
    fee
    fii
    fuu
    I want to put "MY TEXT" so I did the script below:

    Code:
    #!/bin/bash
    
    cat /root/tmp/text > /root/tmp/text.tmp
    echo "MY TEXT" > /root/tmp/text
    cat /root/tmp/text.tmp >> /root/tmp/text
    rm -f /root/tmp/text.tmp
    Is there other way I could do this?

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    477
    using "tac" you could save one line:

    #!/bin/bash
    echo "MY TEXT" >> /root/tmp/text
    tac /root/tmp/text > /root/tmp/text.tmp
    mv /root/tmp/text.tmp /root/tmp/text
    the sun is new every day (heraclitus)

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    put on top
    Code:
    sed '1 i text' file
    put at line number 10
    Code:
    sed '10 i text' file

  4. #4
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    this may work too:

    Code:
    sed '1i\
    your text goes here' file_name > file_name

Posting Permissions

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