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
I should be getting three files, which is nothing but the same as wc -l of hello1.txt and hello2.txt.
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.
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!
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
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?
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
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 .