Replace two values in a file with input from two different files

Hi,

I was having the following issue

cat input

hello1, my name is unix.com. I am awesome. Hope you know this, hello2!
cat hello1.txt
Hi Friends
Hi Folks
Hi Well-Wishers
cat hello2.txt
Honey
Sweety
Darling

Required Output

  1. I should be getting three files, which is nothing but the same as wc -l of hello1.txt and hello2.txt.
  2. Each line in hello1.txt is related to the same line in hello2.txt. Which means, Hi Friends is related to Honey. Hi Folks is related to Sweety and Hi Well-Wishers is related to Darling.
  3. The three files will be like this
cat output1
Hi Friends, my name is unix.com. I am awesome. Hope you know this, Honey!
cat output2
Hi Folks, my name is unix.com. I am awesome. Hope you know this, Sweety!
cat output3
Hi Well-Wishers, my name is unix.com. I am awesome. Hope you know this, Darling!

Thanks in advance.

try

 awk '{A[++c] = $0} END { for ( i = 1; i <=3; i++ ) {print A","A[c]A[i+3]"!"}}' hello1.txt hello2.txt input

output

[Makarand] # awk '{A[++c] = $0} END { for ( i = 1; i <=3; i++ ) {print A","A[c]A[i+3]"!"}}' hello1.txt hello2.txt input
Hi Friends,my name is unix.com. I am awesome. Hope you know this, Honey!
Hi Folks,my name is unix.com. I am awesome. Hope you know this, Sweety!
Hi Well-Wishers,my name is unix.com. I am awesome. Hope you know this, Darling!

for 3 files

awk '{A[++c] = $0} END { for ( i = 1; i <=3; i++ ) {print A","A[c]A[i+3]"!"}}' hello1.txt hello2.txt input| | sed -n '1,1p'  > output1
awk '{A[++c] = $0} END { for ( i = 1; i <=3; i++ ) {print A","A[c]A[i+3]"!"}}' hello1.txt hello2.txt input| sed -n '2,2p' > oouput2
awk '{A[++c] = $0} END { for ( i = 1; i <=3; i++ ) {print A","A[c]A[i+3]"!"}}' hello1.txt hello2.txt input| sed -n '3,3p' > output3
1 Like

Makarand,

Thanks for the reply.

There are multiple hello1s and hello2s in the input file at on different lines. Like a paragraph filled with hello1 and hello2 and other text.

I just gave you a simple example.

Your code is not replacing all the instances of hello1 and hello2 and in addition it is printing the hello1 and hello2 into the output file. Any pointers, please?

Thanks a lot for your time.

Could you please post the code you have now - the one that fails?
Then post exactly the output you want to get.

I do not get why you say wc -l should match for each file after you run your command.

Jim,

I was talking about the hello1.txt and hello2.txt file record numbers and not the output file counts.

Okay - given what you said you want all three #1 records from each file output onto the same line in the new file. New file has three lines, each the result of concatenation.

That is what related means. In the interest of getting the thread going this code does that. But I'll bet it is not what you meant. Lacking examples of output you therefore get:

awk '{arr[FNR]=arr[FNR] $0} END {for(i in arr) {print arr}}'  \
    file1 file2 file3 > file4

No, Jim. You got me wrong.

I want to replace hello1 in my input with the content from hello1.txt. And hello2 from my input in hello2.txt.

So, for each line in hello1.txt and hello2.txt, and each presence of hello1 and hello2 in the input, I want separate files.

Thanks for your time.

Perhaps something like this will work:

awk '
FNR == 1 {
        # This is the 1st line in a file; increment file count.
        f++
}
{       # Collect data from input files.
        d[f, cnt[f] = FNR] = $0
}
END {   # Verify that number of lines in the 1st two files are identical...
        if(cnt[1] != cnt[2]) {
                print "1st two files do not have the same number of lines."
                exit 1
        }
        # Verify that we were given 3 input files...
        if(f != 3) {
                printf("Found %d files, expected 3.\n", f)
                exit 2
        }
        # For each set of values found in the 1st two files...
        for(i = 1; i <= cnt[1]; i++) {
                # Set output filename.
                of = "output" i
                # For each line in the 3rd file...
                for(j = 1; j <= cnt[3]; j++) {
                        # Initialize output line to data from the 3rd file.
                        o = d[3, j]
                        # Replace occurrences of hello1 and hello2 with data
                        # from the 1st and 2nd files, respectively.
                        gsub(/hello1/, d[1, i], o)
                        gsub(/hello2/, d[2, i], o)
                        # Write the modified line to the output file.
                        print o > of
                }
                # Close the current output file.
                close(of)
        }
}' hello[12].txt input

As always, if you want to run this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .

1 Like

Don,

Beautiful!!! Absolutely beautiful!

Thanks a lot for your time and this wonderful piece.