LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-26-2009, 05:09 PM   #1
Tuxqi
Member
 
Registered: Apr 2009
Posts: 49

Rep: Reputation: 2
Adding a line to multiple files using Sed


Ok, I have a load of php/html files all structured the same way (about 90).

Each page currently ends with:

Code:
<hr />
</div>
</body>
</html>

but I want to add a new line between <hr /> and </div>

so it now looks like this:

Code:
<hr />
<?php include("../footer.php"); ?>
</div>
</body>
</html>
I suppose an easier way is to delete the last 3 lines and add them to the footer.php file. Altough I am unsure how to do that using sed as well.
 
Old 11-26-2009, 06:13 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
The basic logic behind this is, get to the line where <hr /> is, print that line, then print your required line, then do the rest. Its that simple
Code:
$ awk '/<hr \/>/{print;print "new";next}1' file
in bash
Code:
while read -r line
do
  case "$line" in
     *"<hr />"*) 
        echo "$line"
        echo "my new line"        
        ;;
     *) echo "$line"
  esac
done < "file"

Last edited by ghostdog74; 11-26-2009 at 06:14 PM.
 
Old 11-27-2009, 02:01 AM   #3
Tuxqi
Member
 
Registered: Apr 2009
Posts: 49

Original Poster
Rep: Reputation: 2
problem with that is, that there are a few <hr />, the thing that distinguishes the one I want to add a line underneath is that </div> comes underneath this particular <hr />.
 
Old 11-27-2009, 02:17 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

This seems to be working:

sed '/<\/div>/,/<\/body>/s%\(<\/div>\)%<?php include("../footer.php"); ?>\n\1%' infile

Testrun:
Code:
$ cat infile
<hr />
<td>
</td>
<hr />
<br>
<asdfasdf />
<hr />

<hr />
</div>
</body>
</html>


$ sed '/<\/div>/,/<\/body>/s%\(<\/div>\)%<?php include("../footer.php"); ?>\n\1%' infile
<hr />
<td>
</td>
<hr />
<br>
<asdfasdf />
<hr />

<hr />
<?php include("../footer.php"); ?>
</div>
</body>
</html>
If this works for you you need to add the -i switch to sed (sed -i '/.......) to make the change in place. A simple loop will make sure you process all the files.

Hope this helps.
 
Old 11-27-2009, 01:58 PM   #5
Tuxqi
Member
 
Registered: Apr 2009
Posts: 49

Original Poster
Rep: Reputation: 2
That's nearly perfect but its added the line before every instance of the </div> tag.
 
Old 11-27-2009, 02:27 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Yes, that's where it should be if I look at example in post #1:

Quote:
but I want to add a new line between <hr /> and </div>

so it now looks like this:

Code:
<hr />
<?php include("../footer.php"); ?>
</div>
</body>
</html>
My output is the same as your requirement........

If your example isn't correct or your requirements are not as stated it is kinda hard to come up with something that works. Please elaborate what it is you actually want.
 
Old 11-27-2009, 02:36 PM   #7
Tuxqi
Member
 
Registered: Apr 2009
Posts: 49

Original Poster
Rep: Reputation: 2
Sorry for not being 100% clear,

what I was saying was that the part of the code is unique as there is only one part of it where the <hr /> appears and then the </div> comes next.

thats why I said between those two as its unique to the rest of the code.
 
Old 11-27-2009, 02:57 PM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Probably me, but I really don't get what it is you are trying to say.

Could you provide a relevant example of your input file? And at least include what triggered the false hits or other problems.
 
Old 11-27-2009, 03:01 PM   #9
Tuxqi
Member
 
Registered: Apr 2009
Posts: 49

Original Poster
Rep: Reputation: 2
this is what happened.


<br />
<?php include("../footer.php"); ?>
</div>
<hr />
blah blah blah text text blah blah blah
<hr />
<?php include("../footer.php"); ?>
</div>
</body>
</html>

It also added a line between <br /> and </div>, which is an unwanted result.
 
Old 11-27-2009, 03:23 PM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Ok, I get it

lineNo=$((`sed -n '$=' infile`-2)) ; sed "${lineNo}s%\(<\/div>\)%<?php include("../footer.php"); ?>\n\1%" infile

A nice one liner

What it does:

1) Determine the amount of lines in the file (sed -n '$=' infile),
2) subtract 2 (To get the linenumber where the last </div> tag is), store in vatiable lineNo
3) replace on that specific line (sed "${lineNo}s%\(<\/div>\)%<?php include("../footer.php"); ?>\n\1%" infile)

1 assumption: All pages end with </html>. If any empty/other lines are after that my solution will fail.

Not an elegant solution, but I hope it does what you want!
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a Comma to the end of every nth line in Vi (or sed). Euler2 Linux - Newbie 6 10-12-2009 09:38 AM
SED how to find multiple patterns on a single line yaazz Programming 9 07-31-2009 04:20 AM
how do I write a script using sed to delete the first and last line in multiple files Rikki D Programming 4 05-02-2008 11:01 PM
sed to extract multiple matches in a line? mhoch3 Linux - Software 8 08-01-2005 03:32 PM
sed - multiple matches on the same line mjoc27x Programming 6 04-17-2003 07:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:27 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