Find the answer to your Linux question:
Results 1 to 3 of 3
greetings i have a large section of html i need to add to the top of a file. i would like to retain the formatting of the html so that ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2012
    Posts
    31

    add block of code to top of file in bash


    greetings
    i have a large section of html i need to add to the top of a file.
    i would like to retain the formatting of the html so that it looks proper. How can i do this? thanks

  2. #2
    Linux Guru
    Join Date
    Dec 2013
    Location
    Victoria, B.C. Canada
    Posts
    1,655
    I can't say that bash seems like an appropriate tool for this but:

    Code:
    add=0
    while read line
    do
    
         if (( $add ))
         then
              echo <YOUR_CODE_HERE>
              add=0
         else
              echo "$line"
         fi
    
         if [[ $line =~ (.*<\s*body[^>]*>)|(<\s*/\s*body\s*\>.+) ]]
         then
             add=1
         fi
    done <index.html
    Something like that will work if you can presumptions about the formatting.

    *also presuming by top of the file you meant top of the body
    Last edited by gregm; 06-09-2014 at 10:19 PM. Reason: Fixed order of ifs

  3. #3
    Linux Enthusiast
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    518
    I have some scripts that build html files. They start with a few standard input files:
    Code:
    head.html.in
    body.html.in
    tail.html.in
    build-index.sh
    Then build-index.sh cats head.html.in >, say, index.html.

    Then it parses body.html.in >> index.html whatever way I need to, often creating custom bits. Usually it's for creating an index listing of a subirectory, of maybe thumbnails and their links to the real files or something like that.

    Then, finally, cats tail.html.in >> index.html. Done.

    Works good for me.

  4. $spacer_open
    $spacer_close

Posting Permissions

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