I was looking for a way to combine two files that are uneven, I know there is a solution with awk but I am unable to modify it to fit my need. I.E.
File 1
a
b
c
d
File 2
1
2
Solution
a:1
b:2
c:1
d:2
if the first file is 10 words and the second is only 3 words, repeat the 3 words until the end of the first file EOF, and im sure theres a delimiter flag with ':' in there.
My best attempt was:
paste -d file1 /dev/null file2 > new_file
But that only put the 1,2 in the new list but didn't repeat.
For the given sample data, you may try this awk:
awk 'FNR == NR{a[++n]=$1; next} {print $1 ":" a[(FNR-1) % n + 1]}' file2 file1
a:1
b:2
c:1
d:2
With your shown samples, please try following awk code.
awk '
FNR==NR{
arr[++count1]=$0
next
}
{
print $0":"arr[++count2]
count2=(count2==count1?0:count2)
}
' file2 file1
Explanation: Adding detailed explanation for above code.
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition FNR==NR when file2 is being read.
arr[++count1]=$0 ##Creating arr array with index of count1(increasing it with 1) and value is current line.
next ##next will skip all further statements from here.
}
{
print $0":"arr[++count2] ##Printing current line colon and value of arr with index of ++count2.
count2=(count2==count1?0:count2) ##Setting count2 to 0 if its eqal to count1 else keeping it as it is.
}
' file2 file1 ##Mentioning Input_file names here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With