Search code examples
shellunixreplacehp-ux

Shell - replace only number on specific line with number from another file


I am working on a Shell script and I need to replace only number on line 13 with a number from another file.

file1:

line1
line2
...
Text: 95%
...

file2:

98.4256

The result should look like this:

file1:

...   
Text: 98.4256%
...

Basically I need to replace the number before % in file1 on line 13 with a number from file2 (the number in file2 is on line 1).

Thanks in advance for any tips.


Solution

  • sed "4 s/:.*/: $(cat file2)%/" file1
    line1
    line2
    ...
    Text: 98.4256%
    ...
    

    Change 4 to any other number of your requirement.

    Contents of file1

    cat file1
    line1
    line2
    ...
    Text: 95%
    ...
    

    Contents of file2

    cat file2
    98.4256