Need to add code to hundreds of .html files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to add code to hundreds of .html files
# 1  
Old 08-21-2014
Need to add code to hundreds of .html files

Need assistance to add code to hundreds of .html

Code will look like below and needs to be added below <html> tag:

Code:
<script>

Some .js code here

</script>

This will be used in Fedora release 7 (Moonshine).

I will appreciate any type of help and/or orientation.

Thank you!
# 2  
Old 08-21-2014
try
Code:
for file in $(ls -ltr *.html | awk '{ print $9 }')
do 
 for filedata in $(cat $file) 
 do
  if [ $filedata == "<html>" ]; then
   echo "<script>" >> $file
   echo "Some .js code here" >> $file
   echo "</script>" >> $file
  fi 
 done
done


Last edited by Makarand Dodmis; 08-21-2014 at 11:46 AM..
# 3  
Old 08-22-2014
Hi Makarand,
Why use:
Code:
for file in $(ls -ltr *.html | awk '{ print $9 }')

instead of:
Code:
for file in *.html

The second form will not fail due to ARG_MAX limits nor filenames containing whitespace characters; doesn't need to start up a subshell, doesn't need to invoke ls or awk; and will run faster.

All expansions of $file and $filedata also need to be quoted.

It also seems strange that this code replaces everything in every input file with one copy of the text that was supposed to be added after every occurrence of the word <html> in the corresponding input file instead of adding that text to the existing file in the specified spot(s).

Hi Ferocci,
Is the text to be added contained in a file, or is it just a string in your script?

I don't have time to give a complete proposal tonight, but if I was doing this I'd probably use ed -s "$file" in a loop instead of reading the files line by line in the shell (and I certainly wouldn't use echo to copy the results back to the files being processed.

Do you need more help with this?
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 08-22-2014
Below code will create new files (*.out) with required changes
Code:
for i in *.html
do
awk '/^[ \t]*<[Hh][Tt][Mm][Ll][ >]/ {print; getline
 print "<script>"
 print "Some .js code here"
 print "</script>"}1' "${i}" > "${i}.out"
done

If you want to make the changes to the file
Code:
for i in *.html
do
awk '/^[ \t]*<[Hh][Tt][Mm][Ll][ >]/ {print; getline
 print "<script>"
 print "Some .js code here"
 print "</script>"}1' "${i}" > "${i}.out"
mv "${i}.out" "${i}"
done

# 5  
Old 08-22-2014
Try also
Code:
sed -r '/<(html|HTML)/ r insertfile' *.html

given your script is in insertfile. This works, but is not tested against all possible html- pages. Refine/adapt if need be.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Hundreds of files need manual preparation. Does shell script could do it automatically?

Hello friends, I have hundreds files in hand, which need extract some data from logs and read these data into an input file. Here I will explain in detail using these two files as attached. read some data from .log file and write it into the .in file. **explanation is given inside two... (9 Replies)
Discussion started by: liuzhencc
9 Replies

2. Shell Programming and Scripting

Add Color To html Doc

I have a script which converts a .csv file to html nicely. Trying to add 3 colors, green, yellow and red to the output depending upon the values in the cells. Tried some printf command but just can't seem to get any where. Any ideas would be appreciated. nawk 'BEGIN{ FS="," print ... (7 Replies)
Discussion started by: jimmyf
7 Replies

3. Shell Programming and Scripting

Best way to connect to hundreds of nodes and grep log files

Hi, What will be the best way to connect (ssh) to hundreds of nodes and grep log files parallely from shell. Using for loop seems to be sequential. Are there any shell built in construct which could be used to achieve this? Is the sub shell any good here? (1 Reply)
Discussion started by: agent001
1 Replies

4. Solaris

Renaming hundreds of files at the same time

Hi, I am having issues trying to figure out how to rename files like this: TEST1_B.tt To SQP_CAN_B.tt I have hundreds of files like those, I need to rename them automatically. Any help will be greatly appreciated. Thanks, (5 Replies)
Discussion started by: ocramas
5 Replies

5. Shell Programming and Scripting

HTML code remove

Hello, I have one file which has been inserted intermittently with HTML web page. I would like to remove all text between "<html xmlns="http://www.w3.org/1999/xhtml">" and </html> tags. Can any one please suggest me sed regular expression for it. Thanks (3 Replies)
Discussion started by: nrbhole
3 Replies

6. UNIX for Dummies Questions & Answers

adding hundreds of numbers

i know how to add two numbers using expr, but if i have a file with hundreds of numbers, how do i add them all together, without typing them all one by one? for example, file.txt contains 4 5 6 7 how can i give a command to add them, without typing $ expr `4 + 5 + 6 + 7` (7 Replies)
Discussion started by: FOBoy
7 Replies

7. Shell Programming and Scripting

increment a value at an offset in hundreds very large hex file

I have a lot of very large hex files that I need to change one value at the same offset and save to another file. I have a script that finds each file and just need to put an operator for each file. I think sed might be able to do this but I have not used it before and would like some help. If... (8 Replies)
Discussion started by: Eruditass
8 Replies

8. Shell Programming and Scripting

fetch substring from html code

hello mates. please help me out once again. i have a html file where i want to fetch out one value from the entire html-code sample html code: ..... <b>Amount:<b> 12345</div> ... now i only want to fetch the 12345 from the html document. how to i tell sed to get me the value from... (2 Replies)
Discussion started by: scarfake
2 Replies

9. Shell Programming and Scripting

Using Perl to add hyperlink to html files

Hi , I have written a csh script which will output file named " myoutput.html " displayed below. I am tried to ftp this html file into a local web server so that it can be displayed to public sharing this server and i would want to add a hyperlink to " XX " , " YY " , " ZZ ". Can this be done... (17 Replies)
Discussion started by: Raynon
17 Replies

10. Shell Programming and Scripting

for loops can i loop in hundreds?

I have the following for ((i=100; i<=1000; i++)) do this goes in increments of 1 I need it to go up in increments of 100, how is that done in a for loop? (1 Reply)
Discussion started by: gazz1982
1 Replies
Login or Register to Ask a Question

Featured Tech Videos