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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace a string with contents of a txt file containing multiple lines of strings
# 1  
Old 04-20-2011
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! Smilie
# 2  
Old 04-21-2011
Code:
awk '/kw01/{system("cat file.txt");next}1' file.xml > newfile.xml

# 3  
Old 04-21-2011
Quote:
Originally Posted by Franklin52
Code:
awk '/kw01/{system("cat file.txt");next}1' file.xml > newfile.xml

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


Code:
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!
# 4  
Old 04-21-2011
Use nawk or /usr/xpg4/bin/awk on Solaris.
# 5  
Old 04-21-2011
Quote:
Originally Posted by Franklin52
Code:
awk '/kw01/{system("cat file.txt");next}1' file.xml > newfile.xml

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:
Code:
<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':
Code:
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.
# 6  
Old 04-21-2011
Quote:
Originally Posted by Franklin52
Use nawk or /usr/xpg4/bin/awk on Solaris.
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.
Quote:
<Cell ss:StyleID="s78"><Data ss:Type="String">kw01</Data></Cell>
after executing nawk '/kw01/{system("cat fatal_alerts.txt");next}1' template.xml > test.xml, this is what happens with the line above:

Quote:
no rows selected
the tags for 'kw01' were removed. while i was expecting something like:
Quote:
<Cell ss:StyleID="s78"><Data ss:Type="String">

no rows selected

</Data></Cell>
this is nerve wrecking. Smilie
# 7  
Old 04-21-2011
Quote:
Originally Posted by 4dirk1
this is nerve wrecking
It shouldn't be. Did you try my example from above? It should work ok for you. Here it is again:
Code:
nawk -v f="kw01.txt" 'BEGIN {while (getline < f) txt=txt $0 "\n"} /kw01/ {sub("kw01", txt)} 1' file.xml > newfile.xml

Regards,
Mark.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string with multiple lines

Hello Guys, I need to replace a string with multiple lines. For eg:- ABC,DEF,GHI,JKL,MNO,PQR,STU need to convert the above as below:- ABC,DEF, GHI1 GHI2 GHI3, JKL,MNO, PQR1 PQR2 PQR3, STU i have tried using code as:- (2 Replies)
Discussion started by: jassi10781
2 Replies

2. Shell Programming and Scripting

sed - Replace string with file contents

Hello, I have two files: file1 and file2 file1 has the following info: --- host: "localhost" port: 3000 reporter_type: "zookeeper" zk_hosts: - "localhost:2181" file2 contains an IP address (1.1.1.1) What I want to do is replace localhost with 1.1.1.1, so that the... (4 Replies)
Discussion started by: Jay Kah
4 Replies

3. Shell Programming and Scripting

Find multiple strings and replace single string

Hi, following Perl code i used for finding multiple strings and replace with single string. code: #!/usr/bin/perl my @files = <*.txt>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)'; my $replaceStr =... (2 Replies)
Discussion started by: chettyravi
2 Replies

4. Shell Programming and Scripting

Replace the .txt file between two strings in XML file

Hi i am having XML file with many number of lines,I need to replace between two strings with .txt file using awk. For ex <PersonInfoShipTo ------------------------------ /> My requirement is to replace the content between <PersonInfoShipTo ------------------------------ /> help me. Thanks... (9 Replies)
Discussion started by: Padmanabhan
9 Replies

5. Shell Programming and Scripting

Replace Contents between 2 strings in a file with contens of another file

Please I want to replace all the contents beween "Section" and "Ensection" in file1 with all contents in file2. Example: file1: Section "Screen" DefaultDepth 24 SubSection "Display" Depth 8 ViewPort 0 0 Modes "1024x768" "800x600" "640x480" EndSubsection SubSection "Display" Depth... (9 Replies)
Discussion started by: powelltallen
9 Replies

6. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

7. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

8. UNIX for Advanced & Expert Users

Find and replace txt between two strings in flat file

Hi There... I need to serach and replace strngs in a text file. My file has; books.amazon='Let me read' news.bestseller='xyz' expected output is books.amazon=NONFOUND news.bestseller=NONFOUND Can I first find the text between string1= books.amazon=' and string2= ' (locate the text... (1 Reply)
Discussion started by: Hiano
1 Replies

9. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

10. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies
Login or Register to Ask a Question