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
# 8  
Old 04-21-2011
Quote:
Originally Posted by cambridge
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.
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 ----------

Quote:
Originally Posted by cambridge
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.
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?? Smilie I really really want to learn more about unix scripting. THANKS AGAIN!
# 9  
Old 04-21-2011
Quote:
Last request sir Mark, would you mind explaining your code to me??
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.
This User Gave Thanks to cambridge For This Post:
# 10  
Old 04-21-2011
Code:
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 ----------

Code:
$ cat xml
<xml>
<myfile><contents>kw01</contents></myfile>
</xml>

Code:
$ 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.>

Code:
$ 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>

# 11  
Old 04-21-2011
sir ctsgnb i tried your code and it gave me this error message:

Quote:
bash-2.05$ awk -F"kw02" 'NF>1{printf "%s",$1;for (i=2;i<=NF;i++){system("cat backlogstats.txt");printf "%s",$i ((i==NF)?"\n":z)}}NF==1' template.xml
awk: syntax error near line 1
awk: illegal statement near line 1
what could be wrong??
# 12  
Old 04-22-2011
If you are on SunOS / Solaris, replace "awk" with "nawk"
This User Gave Thanks to ctsgnb For This Post:
# 13  
Old 04-26-2011
Quote:
Originally Posted by ctsgnb
If you are on SunOS / Solaris, replace "awk" with "nawk"
Sir this one also worked! Thanks a lot! would u mind explaining your code sir? Smilie
# 14  
Old 04-26-2011
awk -F"kw01"use the string kw01 as field separator
'NF>1if the number of field is greater than 1 (so in fact it is like "if the kw01 string is found in the line")
{printf "%s",$1;display the first field without going to a new line
for (i=2;i<=NF;i++)for all other field of this line
{system("cat tst");output the txt file
printf "%s",$i ((i==NF)?"\n":z)} and concatenate the scanned field, if the scanned field is the last field of the line, then add the newline character to that field
}NF==1'also output the lines that do not contains the kw01 string
This User Gave Thanks to ctsgnb For This Post:
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