replace a string with contents of a txt file containing multiple lines of strings

Hello everyone,

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! :frowning:

awk '/kw01/{system("cat file.txt");next}1' file.xml > newfile.xml

sir i tried the code above. It gave me this error:

bash-2.05$ awk '/kw01/{system("cat file.txt");next}1' template.xml > test.xml
awk: syntax error near line 1
awk: bailing out near line 1

what could be wrong? Thank you so much!

Use nawk or /usr/xpg4/bin/awk on Solaris.

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.

Consider the following pseudo input:

<xml>
<myfile><contents>kw01</contents></myfile>
</xml>

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':

nawk -v f="kw01.txt" 'BEGIN {while (getline < f) txt=txt $0 "\n"} /kw01/ {sub("kw01", txt)} 1' file.xml > newfile.xml

Best regards,
Mark.

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:

this is nerve wrecking. :frowning:

It shouldn't be. Did you try my example from above? It should work ok for you. Here it is again:

nawk -v f="kw01.txt" 'BEGIN {while (getline < f) txt=txt $0 "\n"} /kw01/ {sub("kw01", txt)} 1' file.xml > newfile.xml

Regards,
Mark.

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?? :slight_smile: 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.

1 Like
awk -F"kw01" 'NF>1{printf "%s",$1;for (i=2;i<=NF;i++){system("cat file.txt");printf "%s",$i ((i==NF)?"\n":z)}}NF==1' file.xml

---------- Post updated at 03:31 PM ---------- Previous update was at 03:27 PM ----------

$ cat xml
<xml>
<myfile><contents>kw01</contents></myfile>
</xml>
$ cat tst
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.>
$ awk -F"kw01" 'NF>1{printf "%s",$1;for (i=2;i<=NF;i++){system("cat tst");printf "%s",$i ((i==NF)?"\n":z)}}NF==1' xml
<xml>
<myfile><contents>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.>
</contents></myfile>
</xml>

sir ctsgnb i tried your code and it gave me this error message:

what could be wrong??

If you are on SunOS / Solaris, replace " awk " with " nawk "

1 Like

Sir this one also worked! Thanks a lot! would u mind explaining your code sir? :rolleyes: