Results 1 to 3 of 3

Thread: How to add data at a specific place to a file

  1. #1
    Join Date
    Apr 2011
    Beans
    14

    Smile How to add data at a specific place to a file

    for example if I have the following data in a fie
    <!DOCTYPE html>
    <html>
    <body>


    <h1>What Can JavaScript Do?</h1>
    <p id="demo">JavaScript can change HTML content.</p>

    </body>
    </html>

    and I want to add "<food>" between the header and the body

    <!DOCTYPE html>
    <html>
    <body>


    <h1>What Can JavaScript Do?</h1>
    <food>
    <p id="demo">JavaScript can change HTML content.</p>

    </body>
    </html>



    How can I issue a command to do this. I found online that I can use awk or sed but all the examples I found are for printing data and not adding it to a file. Can anybody help me with this one?

  2. #2
    Join Date
    Aug 2013
    Beans
    336

    Re: How to add data at a specific place to a file

    Is the sed/awk output the whole file? In linux command line you can redirect anything output to the screen to a file like this. [code]command > file [\code]

    Where command put the sed command & arguments and in file put f.e. edit.html but please not the same filename as the file you are trying to alter. If the edited file is correct you can then delete the original and rename it.

  3. #3
    Join Date
    May 2010
    Location
    uk
    Beans
    9,249
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: How to add data at a specific place to a file

    Hi

    You can insert a line after a line that contains a pattern match.

    You need the -i (in-place) modifier for sed.

    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <h1>What Can JavaScript Do?</h1>
    <p id="demo">JavaScript can change HTML content.</p>
    </body>
    </html>
    Code:
    sed -i '/<body>/a <food>' test.txt
    Code:
    matthew-laptop:/home/matthew:2 % sed -i '/<body>/a <food>' test.txt
    matthew-laptop:/home/matthew:2 % cat test.txt 
    <!DOCTYPE html>
    <html>
    <body>
    <food>
    <h1>What Can JavaScript Do?</h1>
    <p id="demo">JavaScript can change HTML content.</p>
    </body>
    </html>
    matthew-laptop:/home/matthew:2 %
    You really need to read a good book or some online tutorials for sed though as the topic is large.

    Make sure the book and the tutorials are for GNU sed.

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

Tags for this Thread

Bookmarks

Posting Permissions

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