LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-11-2007, 08:57 AM   #1
casperl
LQ Newbie
 
Registered: Jan 2006
Posts: 2

Rep: Reputation: 0
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.
 
Old 05-11-2007, 09:18 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I'll take a shot...

Code:
find /yourDirTree  -name "*.html" -exec sed -i 's/<BODY.*>/<body><p>Hello World<\/p>/ig' {} \;
--- rod.

Last edited by theNbomr; 05-11-2007 at 09:19 AM.
 
Old 05-11-2007, 09:45 AM   #3
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Or, if you don't want to replace the <body> tag (for example, if you had <body color="black"> or similar):

Code:
find path/to/dir/ -name *.html -exec sed -i 's/\(<body.*>\)/\1\
<p>Hello, world<\/p>/' {} \;
Note that the line break after "\1\" is intentional, to put the <p>Hello, world</p> on its own line.
 
Old 05-11-2007, 03:18 PM   #4
casperl
LQ Newbie
 
Registered: Jan 2006
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks. It worked perfectly
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatically Copying files from the ftp directory into the html directory swatward Linux - General 3 04-17-2005 10:55 PM
script needed: directory -> html with links to all files in dire flamesrock Linux - Software 2 03-22-2005 01:47 AM
apache server setup and how to place files in the var/www/html directory dramous Linux - Newbie 7 09-28-2004 04:18 AM
apache server setup and how to place files in the var/www/html directory dramous Linux - Software 3 09-25-2004 02:38 AM
missing html files in my home directory scottsteib Linux - Newbie 0 07-29-2003 09:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration