Home > Mobile >  Most efficient way to use first column of file1 as prefix for all lines in file2
Most efficient way to use first column of file1 as prefix for all lines in file2

Time:12-17

I have two files with equal amount of lines. I want to add each value of column 1 to become the prefix of each line in file 2 and separate each value of file2 by one whitespace. File2 is very large and has more than 70 million columns.

Example: Input file 2

10000
10019

Input file 1

Ind1
Ind2

Output

Ind1 1 0 0 0 0 
Ind2 1 0 0 1 9

Q: How can this be done efficiently?

EDIT I : I already looked for solutions to add different prefixes to each line e.g. here but was unable to adjust the solution so that I can iterate over the values of the first column of another file.

EDIT II : Using the answer from @Gilles I came up with this:

awk ' { print $1 } ' file1 <(sed 's/./& /g' file2) > output 

CodePudding user response:

$ paste -d '' file2 <(sed 's/./& /g' file1) 
Ind1 1 0 0 0 0 
Ind2 1 0 0 1 9 

CodePudding user response:

Using any awk:

$ awk '{head=$0} (getline tail < "file2") > 0{gsub(/./," &",tail); print head tail}' file1
Ind1 1 0 0 0 0
Ind2 1 0 0 1 9

or if the whole contents of file1 fit in memory:

$ awk 'NR==FNR{a[FNR]=$0; next} {gsub(/./," &"); print a[FNR], $0}' file1 file2
  • Related