-1
cat file.txt 
TEXT1 TEST2 TEST3
cat other_file.txt 
there are multiple lines1.
there are multiple lines2.
there are multiple lines3

I want the following output :

cat file3.txt 
there are TEXT1 TEST2 TEST3 lines1
there are TEXT1 TEST2 TEST3 lines2
there are TEXT1 TEST2 TEST3 lines3

replace multiple key word with all the content from file on each line hope this is clear

4
  • Can file.txt contain multiple lines? How would the output look like in this case? Can a line in other_file.txt contain multiple more than once? Please edit your question to answer.
    – Bodo
    Jul 26, 2022 at 11:57
  • yes file.txt have multiple lines and other_file.txt contain "multiple" more than once in every line . i hope i am clear now
    – sam
    Jul 26, 2022 at 12:02
  • Please show an additional example that has more than one line in file.txt and more than one occurrence of multiple in other_file.txt and the expected output for this case. Please clarify if the last line in file.txt is terminated with a newline or not and what should happen with the newline character(s).
    – Bodo
    Jul 26, 2022 at 12:13
  • 1
    Please don't post the same question again: instead edit your existing question.
    – terdon
    Jul 26, 2022 at 14:40

2 Answers 2

0

Untested:

sed '1h;1d;G;s/multiple\(.*\)\n\(.*\)/\2\1/;P;d' file.txt other_file.txt > file3.txt

Do I need to explain?

4
  • giving output like below
    – sam
    Jul 26, 2022 at 12:05
  • giving output like below there are TEXT1 lines1. there are TEXT1 lines2. there are TEXT1 lines3. i want output like below there are TEXT1 TEST2 TEST3 lines1 there are TEXT1 TEST2 TEST3 lines2 there are TEXT1 TEST2 TEST3 lines3
    – sam
    Jul 26, 2022 at 12:31
  • @user535186 The sed solution provided works perfectly in my system sed (GNU sed) 4.8. If the output in your system does not provide the required results, maybe you are using a different sed version. Jul 26, 2022 at 13:42
  • There can't be no version dependence in here. Nothing would remove some parts of the line without reason. I suspect the OP actually uses different data.
    – Philippos
    Jul 26, 2022 at 14:31
0

With GNU Awk 5.1.0, bellow code will produce what you describe.

cat file1
TEXT1 TEST2 TEST3

cat file2
there are multiple lines1.
there are multiple lines2.
there are multiple lines3.

awk 'NR==FNR{a=$0;next}{$3=a}1' file1 file2
there are TEXT1 TEST2 TEST3 lines1.
there are TEXT1 TEST2 TEST3 lines2.
there are TEXT1 TEST2 TEST3 lines3.

Short explanation:
NR==FNR : Quick way of awk to compare NR (global record number) to FNR (record number of processed file). NR equals to FNR only for the very first file.

a=$0: $0 holds the whole record / line and this is stored in a variable called a

next: Says to awk to read the next record/line of the file.

So this part of code is executed only for the very first file fed to awk, meaning file1:
NR==FNR{a=$0;next}
PS: If your first file has more than one lines, then variable a will hold the contents of the last record/last line of file1.

This piece of code is executed on the second file (file2):
{$3=a}1

$3=a: Replaces $3 (3rd field of input) with the contents of variable a. If the word multiple is not always on the 3rd field of input line of file2 , obviously this solution will fail.
Alternatively you can use something like {sub("multiple",a)}1 to literally replace the word multiple with the contents of variable a.

1 : Just prints the current record/line

Not the answer you're looking for? Browse other questions tagged .