Inserting data - from one file to another?

Hi Experts,

I have a config file (file1) & a data file (file2) :

  • The file1 I want to modify : to replace "2nd fields c7? & m7? from the data from file2,
    -The below CPU line fields need to fill by file2's 1st colmns correspoding data.
  • The below MEM lines to be replaced by file2's 2nd columns corresponding data.

file1

<tr>
  <td> CPU(%) </td>
  <td> c75 </td>
  <td> c76 </td>
  <td> c77 </td>
  <td> c78 </td>
  <td> c71 </td>
</tr>
<tr>
  <td> MEM(%) </td>
  <td> m75 </td>
  <td> m76 </td>
  <td> m77</td>
  <td> m78 </td>
  <td> m71 </td>
</tr>

file2 (having the data)

1.86% 	51.98%
11.63% 	47.61%
0.26% 	45.53%
0.76%	47.27%
18.6 	37.4

The desired output to be look like this:

<tr>
  <td> CPU(%) </td>
  <td> 1.86%  </td>
  <td> 11.63% </td>
  <td> 0.26%  </td>
  <td> 0.76%  </td>
  <td> 18.6   </td>
</tr>
<tr>
  <td> MEM(%) </td>
  <td> 51.98% </td>
  <td> 47.61% </td>
  <td> 45.53% </td>
  <td> 47.27% </td>
  <td> 37.4   </td>
</tr>

Thanks,

---------- Post updated at 04:37 PM ---------- Previous update was at 04:32 PM ----------

Dear moderator: can you please correct the subject line to: Inserting data - from one file to another?

Just curious, why don't you simply create a new file rather than replacing?

awk '
        {
                C[NR] = $1
                M[NR] = $2
        }
        END {
                print "<tr>"
                print OFS "<td> CPU(%) </td>"
                for ( i = 1; i <= NR; i++ )
                        print OFS "<td> " C " </td>"
                print "</tr>"

                print "<tr>"
                print OFS "<td> MEM(%) </td>"
                for ( i = 1; i <= NR; i++ )
                        print OFS "<td> " M " </td>"
                print "</tr>"
        }
' OFS='\t' file2
1 Like

This

awk     'NR==FNR        {CPU[NR]=$1; MEM[NR]=$2; next}
         /c7./          {$2=CPU[++c]}
         /m7./          {$2=MEM[++m]}
         1
        ' file2 file1

would do the job for exactly the samples given (formatting excluded). But - what if the No. of lines in file2 don't equate the No. of c7.s and/or m7.s in file1? And, are the c/m- numbers of any significance?

1 Like

Yoda,
>>Just curious, why don't you simply create a new file rather than replacing?

  • I like this idea and took this approach creating a new file, and has less work just using a loop with variable to build the file1 from the data of file2.

The awk solutions are also good, and yet to go through. Thank you all for help and suggestion.

---------- Post updated at 12:50 AM ---------- Previous update was at 12:33 AM ----------

Also, Yoda & RudiC both awk solutions worked and good.
>> RudiC
what if the No. of lines in file2 don't equate the No. of c7.s and/or m7.s in file1?

  • well in 2nd file has the equal number of data for the matching c7 and m7 entries, as my effort for making one HTML file from the cpu & memory usage data from another file of 5 different server, in a 2 row 5 column table. There is another set of entry of server names that is above.

So the table would come with an email in HTML format from the server itself and easy to look in the email body.

s (in short the server names).

             s75 s76 s77 s78 s71
CPU(%)   values...
MEM(%)  values... 

Thanks ..