LinuxQuestions.org
Visit Jeremy's Blog.
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 08-04-2013, 01:39 PM   #1
andrew777
LQ Newbie
 
Registered: Feb 2009
Posts: 14

Rep: Reputation: 0
How to copy contents of a text file into another file at a specific location


Hi everyone,

I have thousands of html files and I need to put a google plus button in each file. I don't feel like editing thousands of files manually so I want to do it with a linux command but can't figure out how to do it.

I have the following text in a file called googleplus.txt

<!-- Google Plus Button START -->
<div class="g-plusone" data-annotation="inline" data-width="300"></div>
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
<!-- Google Plus Button END -->

I want to be able to place this block of text (above), which is in the file googleplus.txt into each of the thousand files just after a line called:

<!-- AddThis Button END -->

So once the job is done, each file should end up like this:

<!-- AddThis Button END -->
<!-- Google Plus Button START -->
<div class="g-plusone" data-annotation="inline" data-width="300"></div>
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
<!-- Google Plus Button END -->

How can I do this to all html files in a diectory?
Any help would be greatly appreciated.
Thanks
Andy
 
Old 08-04-2013, 01:42 PM   #2
Razaliel
LQ Newbie
 
Registered: Jul 2013
Distribution: Debian testing
Posts: 21

Rep: Reputation: 5
Hi,
is there something else under <!-- AddThis Button END --> ? Or it is the end of file of your html files?
 
Old 08-04-2013, 02:34 PM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
I would use sed, invoked by find

but mine is messy

Code:
find /path/to/inputDir -type f -name "*html" \
-exec sed -i.bak '/<!-- AddThis Button END -->/ a <!-- Google Plus Button START -->\
<div class="g-plusone" data-annotation="inline" data-width="300"></div>\
<script type="text/javascript">\
(function() {\
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;\
po.src = 'https://apis.google.com/js/plusone.js';\
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);\
})();\
</script>\
<!-- Google Plus Button END -->' {} ';'
edit, I added .bak to -i ( so you get a back up )

Last edited by Firerat; 08-04-2013 at 02:38 PM.
 
Old 08-04-2013, 02:48 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
solution with awk

Code:
for input in $(find /path/to/inputDir -type f -name "*html" );do
     tempOutput="${input}.temp"
     awk '/<!-- AddThis Button END -->/{print;system("cat /path/to/googleplus.txt")};!/<!-- AddThis Button END -->/{print}' "$input" > "$tempOutput"
     cp "$input" "${input}.bak"
     mv "${tempOutput}" "${input}"
done

Last edited by Firerat; 08-04-2013 at 02:51 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
File Rename Based on Text Within File in a Particular Location Nargousias Linux - Newbie 3 06-11-2013 02:50 PM
How to Copy text from file set & place text into another file. frinks Linux - General 2 09-26-2012 10:59 AM
[SOLVED] Using Expect/TCL to move contents of a text file to an executable file jefsa Linux - Newbie 4 04-20-2012 10:43 AM
[SOLVED] read a text file distrubute its contents on different text files magische_vogel Programming 13 02-26-2011 06:51 PM
write text to specific location in file mcbenus Linux - Desktop 3 02-28-2008 06:40 AM

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

All times are GMT -5. The time now is 06:53 PM.

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