Sponsored Content
Full Discussion: Text file insertion via sed
Top Forums Shell Programming and Scripting Text file insertion via sed Post 302584181 by Trapper on Thursday 22nd of December 2011 10:37:04 AM
Old 12-22-2011
Text file insertion via sed

I have 800+ html files that need to have a javascript added to them in the head. I can do the looping, setting filenames as variables, etc. but I cannot figure out how to insert my javascript file into the html.

My javascript is in a file named jsinsert.txt

It basically has this format: (snipped to save space)

Code:
<script type="text/javascript">
<!--
function printContent(id){
str=document.getElementById(id).innerHTML
newwin=window.open('','printwin','left=100,top=100,width=400,height=400')
newwin.document.write('<HTML>\n<HEAD>\n')
newwin.document.write('<TITLE>Print Page</TITLE>\n')
newwin.document.write('<script>\n')
newwin.document.write('function chkstate(){\n')
newwin.document.write('if(document.readyState=="complete"){\n')
newwin.document.write('window.close()\n')
newwin.document.close()
}
//-->
</script>

The above code needs to be inserted just prior to </head>
I've tried various things but I am not very fluent with sed. Here's an example:

Code:
sed '/<\/head>/i\ jsinsert.txt' <myfile.html >myfile.html.tmp
mv myfile.html.tmp myfile.html

This fails. It actually puts the words jsinsert.txt just before the </head> rather than the contents of the jsinsert.txt file. Whatever, I've tried several things but cannot get the text to insert.

As I said previously. I can do the looping through the files, extract the filenames I am working with, etc. I just cannot properly insert the contents of the jsinsert.txt file just prior to the </head> tag. Any ideas about how I accomplish that?
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove & insertion of data in a same file

I am iterating record by record through a file as below, A,B A,C A,D B,E B,F E,G E,H The same file should look like in the final output as below, A,B B,E E,G E,H B,F A,C A,D (10 Replies)
Discussion started by: videsh77
10 Replies

2. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

3. Shell Programming and Scripting

insertion of text from sed script

Hi , This is my first thread ; facing some issues withmy sed script I need to insert the code from my log file which is between two keywords. content is like this ........ log ############################ log1 log2 231 "Ban" "tom" and the line one of the cross line friend... (2 Replies)
Discussion started by: shalini_008
2 Replies

4. Shell Programming and Scripting

Insertion in a file

Hi All, I want to insert a line just befor the lst line in a file. please can anyone advise. Cheers, Shazin (3 Replies)
Discussion started by: Shazin
3 Replies

5. Shell Programming and Scripting

Operations on a file with Deletion , Modification and Insertion of lines

Hi All , Given a file , I need to delete , modify and insert lines matching certain patterns in that file using shell scripting. e.g. If a file FILE1 has following content : CUST.ABC.DEF = CUST.ABC.DEF * CUST.ABC.DEF PRINTF(CUST.ABC.DEF) CUST.ABC.DEF = mid(CUST.ABC.DEF,10.34)... (5 Replies)
Discussion started by: swapnil.nawale
5 Replies

6. Shell Programming and Scripting

Insertion New line whilst reading the text file

Hi, For the text file let us say t.txt having the statements as below. filename : t.txt. Contents : This is first string1 This is first string2 This is first string3 The output of the file should have newline. How to introduce the new line so that the output be as follows ... (5 Replies)
Discussion started by: krackjack
5 Replies

7. Shell Programming and Scripting

Need help in using sed/awk for line insertion in xml

Hello, I have two text files (txt1 and txt2). txt1 contains many lines with a single number in each line. txt2 (xml format) contains information about the numbers given in txt1. I need to insert one line in txt2 within the scope of each number taken from txt1. Sample problem: txt1: 12 23... (1 Reply)
Discussion started by: shekhar2010us
1 Replies

8. UNIX for Advanced & Expert Users

Insertion of Null Character while writing into file

I have a huge file with record length around 5000 characters. There is an ETL tool datastage which is writing this data to the file(AIX server). At position 4095 i have seen NULL Character(^@). when i am using this command "head -1 file_nm | sed 's/\000//g'" --- the output is displaying... (3 Replies)
Discussion started by: issaq84mohd
3 Replies

9. Shell Programming and Scripting

Awk/sed problem to write Db insertion statement

Hi There, I am trying to load data from a csv file into a DB during our DB migration phase. I am successfully able export all data into a .csv file but those have to rewritten in terms insert statement which will allow for further population of same data in different DB My exiting csv record... (6 Replies)
Discussion started by: bhaskar_m
6 Replies

10. UNIX for Beginners Questions & Answers

UNIX shell script for matrix insertion into a file

I have a script which is extracting data from a database in an excel file in below given format, date_time calling_app1 count of requests date calling_app x 34 date calling_app y 1034 I want to write a script which will write data into a file like this ... (3 Replies)
Discussion started by: TNT47
3 Replies
wml::std::tags(3)						     EN Tools							 wml::std::tags(3)

NAME
wml::std::tags - Standard Support Tags SYNOPSIS
#use wml::std::tags # Pass 2: advanced if constructs <ifdef varname thenval elseval> # Pass 3: support tag <perl> ... </perl> <say> ... </say> # Pass 4: support tag <m4> ... </m4> # Pass 4: symbols <symbol name value> <symbol name undef> # Pass 5: diversions <dump NAME> <enter NAME> ... <leave> <divert NAME> ... </divert> # Pass 6: support tag <asubst ...> ... </asubst> # Client-Side JavaScript: support tag <javascript [version=..] [language=..] [type=..] [src=...] [defer] [nohead] [nolang]> ... </javascript> # Resynchronize line numbers for more accurate pass 3 warnings <sync-lines> DESCRIPTION
This include file defines some standard support tags one needs all the time. All tags are self-explanatory and briefly mentioned in wml_intro(1), except the last two which are described below. "<javascript>": This tag was primarily a shorthand for "<script type="text/javascript" language="JavaScript">" but it has evolved. If the "<head>" tag is defined and is a complex macro, e.g. when calling "wml::std::page", then Javascript code is automatically passed to this macro and should be diverted to the HEAD section of HTML documents. This diversion preserve lang slices in multi-lingual documents. Those features may be disabled by the ``nohead'' and ``nolang'' attributes. "<sync-lines>": WML tries to print information on input file and line numbers when warnings or errors are reported in passes 2 and 3. Some special sentences containing file names and line numbers are inserted during pass 1. So pass 2 have an accurate knowledge of where warnings appear in input file. But it also filters it without preserving lines, so after pass 2 information is almost unusable. By putting "<sync-lines>" in input file, it forces pass 2 to resynchronize information on line numbers. So for debugging pass 3, it could be of some help to insert "<sync-lines>" before ePerl code. AUTHORS
Ralf S. Engelschall rse@engelschall.com www.engelschall.com Denis Barbier barbier@engelschall.com REQUIRES
Internal: P1, P2, P3, P4, P6 External: -- SEE ALSO
wml(1) EN Tools 2014-04-16 wml::std::tags(3)
All times are GMT -4. The time now is 06:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy