Replace a string with each line from another file repeatedly

I don't know if it's been asked before but seems i gave up seeking.

i have 2 files :
file1.txt

Monday XXXX
Tuesday XXXX
XXXX Wednesday
Thursday XXXX

XXXX is in every lines of file1.txt and i want to replace them with each line in file2.txt:

home
school
cinema

so output file is:

Monday home
Tuesday home
home Wednesday
Thursday home
Monday school
Tuesday school
school Wednesday
Thursday school
Monday cinema
Tuesday cinema
cinema Wednesday
Thursday cinema

I got the file2.txt has many lines so i can't do it by hand. So, i need a solution for it.
Thanks for your time.

while read LINE
do
 sed 's/XXXX/'$LINE'/' file1.txt >>file3.txt
done<file2.txt
cat file3.txt
1 Like

thanks for quickly help.but i got :
-bash: syntax error near unexpected token `done'

Can you include set -x at the beginning of the script and execute? Or execute using sh -x scriptname.sh

1 Like

sorry, i don't know well :

sed 's/XXXX/'$LINE'/' file1.txt >>file3.txt
set -x done file2.txt
cat file3.txt

and i get file3:

Monday
Tuesday
     Wednesday
Thursday
 

Do i miss sth?

Do you use bash or ksh?
Do you have the first line of the script as #!/bin/bash based on the shell?
Can you paste the entire script for us to debug?

I do suggest that you post the complete script.
I do not see the hash bang #!/bin/sh
And you have added set -x before done, not in start of script

set -x goes at the top (may be first line after the hashbang line). Not before done.

cat file2 | xargs -L1 -I{} sed -e 's/XXXX/{}/g' file1
Monday home
Tuesday home
home Wednesday
Thursday home
Monday school
Tuesday school
school Wednesday
Thursday school
Monday cinema
Tuesday cinema
cinema Wednesday
Thursday cinema
# sed 's/XXXX/'$LINE'/' file1.txt>>file3.txt
+ sed s/XXXX// file1.txt
# set -x done file2.txt          
+ set -x done file2.txt
# cat file3.txt                 
+ cat file3.txt
Monday
Tuesday
 Wednesday
Thursday 

set -x is at the wrong place. Could you please post the actual script?

Alternatively you may try the solution posted by rajamadhavan in post #9. It works.

Here's what i get when tried rajamadhavan's post:

Monday home
Tuesday home
 Wednesday
Monday school
Tuesday school
 Wednesday
Monday cinemaol
Tuesday cinema
cinema Wednesday
Thursday cinema

I don't know much about these stuff as i use ssh connecting to my host. so please tell me clearly what am doing wrong.

Cant make out whats going wrong. It works on my system. Please double check the contents of file1.

-Raja

1 Like

content is fine. Think it's just not work for me. Could u explain why i get this on first solution? SOrry if i bother you because i really need this whereas i'm noob myself

#!/bin/bash

set -x
sed 's/XXXX/'$LINE'/' file1.txt >>file3.txt
+ sed s/XXXX// file1.txt
done file2.txt
-bash: syntax error near unexpected token `done'


Where is the

while read LINE
do

?

#!/bin/bash
set -x
while read LINE 
do
  sed 's/XXXX/'$LINE'/' file1.txt >>file3.txt 
done<file2.txt 
cat file3.txt


here is exact file3.txt:

Monday home

Tuesday home

home
 Wednesday
Thursday home
Monday school

Tuesday school

school
 Wednesday
Thursday school

I don't know where's the 'cinema' and why it's break down after each replacements.

Can you also double check the contents of file2.txt ?

1 Like

got:

+ read LINE
+ sed $'s/XXXX/home\r/' file1.txt
+ read LINE
+ sed $'s/XXXX/school\r/' file1.txt
+ read LINE
+ sed $'s/XXXX/cinema\r/' file1.txt
+ read LINE

file3.txt:

Monday home

Tuesday home

home
Wednesday
Thursday home
Monday school

Tuesday school

school
Wednesday
Thursday school
Monday cinema

Tuesday cinema

cinema
Wednesday
Thursday cinema

I could see a \r carriage return character from the data you have posted. Was file2.txt transferred from a windows environment to unix?

If yes, please remove the CRLF (carriage return line feed) characters using command dos2ux or dos2unix . Not sure which one is available in your system.

1 Like

worked finally! thank you!