1

I want to get the value 0.98728E-02 from file 1 that is on line 2 and put in place the value of 1,000 of file 2 that is in line 8. How to do file 1 substitution for file 2?

My file 1:

Genetic variance(s) for effect  2       
0.98728E-02
Genetic variance(s) for effect  3       
0.56818E-02

I need to get the values "0.98728E-02" and put in line in place of the "1.0000" of file 2

My file 2:

DATAFILE 
renf90.dat 
NUMBER_OF_TRAITS 
1 
NUMBER_OF_EFFECTS 
9 
RANDOM_RESIDUAL VALUES 
1.0000
RANDOM_GROUP 
2

I started doing it in "sed" language, but I do not know what else to do ...

sed '23s/1.0000/  .....

Final file that I want to have.

DATAFILE 
renf90.dat 
NUMBER_OF_TRAITS 
1 
NUMBER_OF_EFFECTS 
9 
RANDOM_RESIDUAL VALUES 
0.98728E-02
RANDOM_GROUP 
2

PS: You do not have any constant keywords before or after the lines. PS: Depending on the output file (File 1), I will need to extract two or three values.

2
  • 4
    You could start by providing a sample of the file and your expected output. Jun 3, 2019 at 18:39
  • Please elaborate more on the input and output files. Give a bigger example. Do you need to extract only one value or multiple values? Is there always a constant keyword anywhere in the line before or after the lines you are interested from those two files?
    – seshoumara
    Jun 5, 2019 at 20:36

3 Answers 3

1

I'm sure it's possible with sed, but you could easily do it with awk:

awk 'NR==2 { value=$1 }
     FILENAME=="file2" { if (FNR==8) print value; else print }
' file1 file2 > output_file
0
0

Replace line 8 of file2 with the content of line 2 of file1 with sed:

This changes file2 in place

sed -i'' '8s/.*/'"$(sed -n '2p' file1)"'/' file2

or the same as above, but write changes to file2.new

sed '8s/.*/'"$(sed -n '2p' file1)"'/' file2 > file2.new
0
0
i=`sed -n '2p' file1`

sed  "8s/1.0000/$i/g" file2

output

DATAFILE 
renf90.dat 
NUMBER_OF_TRAITS 
1 
NUMBER_OF_EFFECTS 
9 
RANDOM_RESIDUAL VALUES 
0.98728E-02
RANDOM_GROUP 
2

Note: if you want output should be in file2

sed -i   "8s/1.0000/$i/g" file2
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .