Find the answer to your Linux question:
Results 1 to 4 of 4
I want to modify all html files in a directory (recursively) in the following way: 1. Find starting body tag (<body>) 2. Just after that insert my custom html tags ...
  1. #1
    Just Joined!
    Join Date
    Jan 2007
    Posts
    9

    Modifying HTML files in a directory

    I want to modify all html files in a directory (recursively) in the following way:

    1. Find starting body tag (<body>)
    2. Just after that insert my custom html tags (e.g. <p>Hello World</p>)

    So my aim is just inserting "<p>Hello World</p>" at the top of the page.

    How can i do this? Which command line tools (awk, grep..) should i use? And can you give me some starting point or the command i need to run?

    Thanks.

  2. #2
    Linux Newbie birdman's Avatar
    Join Date
    Mar 2006
    Location
    Ireland
    Posts
    141
    This way should work:

    Code:
    for a in `find . -print | grep .*\.html`
    do
        sed -i "s/<body>/<body><p>Hello World<\/p>/g" $a
    done
    However, this will still insert your tags if they are already there. Check for their existence after <body> in the file if you want to avoid this.

    Regards

  3. #3
    Just Joined!
    Join Date
    Jan 2007
    Posts
    9
    Thanks for the answer birdman.

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by birdman View Post
    This way should work:

    Code:
    for a in `find . -print | grep .*\.html`
    do
        sed -i "s/<body>/<body><p>Hello World<\/p>/g" $a
    done
    Regards
    don't have to use grep,
    Code:
    ....
    find . -type f -name "*.html" -print 
    ....
    however, this can also do it..
    Code:
    find . -type f -name "*.html" -print0|xargs -0 sed -i 's/searchpattern/replace pattern /g'

Posting Permissions

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