Search code examples
linuxfilecopy-paste

Copy a specific field of a file and paste it into another specific field of another existing file - lunx commands


Good afternoon everyone, I have two files like these:

File 1

45.999 60.9999
70.000 23.0090

File 2

DATAFILE
pheno.txt
TRAITS
FIELDS_PASSED TO OUTPUT

WEIGHT(S)

RESIDUAL_VARIANCE

EFFECT
2 cross alpha
EFFECT
1 cross alpha
RANDOM
animal
FILE
pedigree.txt
SNP_FILE
snp_.txt
(CO)VARIANCES

and need to pass the values of file 1 for specific places of file 2 like this:

File 2

DATAFILE
pheno.txt
TRAITS
FIELDS_PASSED TO OUTPUT

WEIGHT(S)

RESIDUAL_VARIANCE
45.999
EFFECT
2 cross alpha
EFFECT
1 cross alpha
RANDOM
animal
FILE
pedigree.txt
SNP_FILE
snp_.txt
(CO)VARIANCES
23.0090

remembering, I want to keep the file 2, just add the file 1 values


Solution

  • set -- $(<file1)
    sed -i "/RESIDUAL_VARIANCE/{n;c$1
    };\${s/[0-9].*/$4/;t;a$4
    }" file2
    

    See also man sed:

    -i edit files in place