ive been trying to replace a string "kw01" in an xml file with the contents of a txt file having multiple lines. im a unix newbie and all the sed combinations i tried resulted to being garbled. Below is the contents of the txt file:
RAISEDATTIME
--------------------
DESCRIPTION
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
18-APR-2011 06:20:32
Fatal error in Application GATE:
<Error code: 68
Caught by: CTCS_BatchSchedulerBO.HandleException
Raise by: CTCS_FTOServerBTM.ValidateMessageCount()
Message: For Interface: FTO:1315001 in File : 190107079531.txt
File Message Count Mismatch. File reported count: 447 actual message count 797.>
Is there a possible way to replace the string "kw01" with the txt file contents above?? please help!
As this is an XML file, I'm not sure one can assume that the text 'kw01' is going to appear on a line by itself. You are, after all, replacing the entire line with the contents of the file.
It would be better to get AWK to replace just the text 'kw01' and not any surrounding characters. This can be achieved as follows, assuming the text file is called 'kw01.txt':
Hi sir i tried nawk, it seemed to work, but it was not the output i was expecting. this xml file opens up as a spreadsheet when sent out to windows environment. Now I placed keywords like 'kw01' on certain cells in this xml file as a marker in which i have to replace this keywords with data taken from SQL queries which i store in txt files (fatal_alerts.txt). Now what happens is, it did replace the keyword 'kw01' with the contents of 'fatal_alerts.txt', but it also removed the tag responsible for printing the contents in the xml file.
before running this command: nawk '/kw01/{system("cat fatal_alerts.txt");next}1' template.xml > test.xml
this is the line in the xml file where 'kw01' is present.
after executing nawk '/kw01/{system("cat fatal_alerts.txt");next}1' template.xml > test.xml, this is what happens with the line above:
the tags for 'kw01' were removed. while i was expecting something like:
OK sir I'll give it a shot. BTW 'kw01' is not a txt file. its mainly a keyword in the xml file in which i set. will it be still fine if i use your command sir Mark?
---------- Post updated at 04:48 PM ---------- Previous update was at 04:36 PM ----------
WOW WOW WOW! YOU SIR ARE A TRUE UNIX GENIUS!! that worked like magic. OMG. thank you so much!
Last request sir Mark, would you mind explaining your code to me?? I really really want to learn more about unix scripting. THANKS AGAIN!
I used the BEGIN section to load the contents of your file into a string variable, using getline with the filename passed in from the shell via the f variable. I used standard pattern matching /kw01/ to find any line that contained the text you needed to replace, and sub() to substitute the text with the string variable set earlier. The 1 at the end of the nawk command simply told AWK to output all lines.