You can use this awk
approach, that firstly reads the file2
storing values in an array and then reads file1
printing the corresponding line:
$ awk 'BEGIN {count=1} FNR==NR {a[++i]=$0; next} /^$/ {count++; next} {print a[count]}' f2 f1
group1
group2
group2
group3
group3
group3
group4
group4
group5
group5
group5
Explanation
BEGIN {count=1}
sets thecount
var as 1 in the beginning.FNR==NR {a[++i]=$0; next}
when readingfile2
(indicated byFNR==NR
), store in the arraya[]
the values of each line./^$/ {count++; next}
when readingfile1
, increment the counter every time an empty line is found. Then skip the line.{print a[count]}
on the rest of lines fromfile1
, print the line store in the arraya[]
with the index stated by the counter.