Inserting data - from one file to another? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Inserting data - from one file to another?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-20-2013
rveri rveri is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 11 July 2014, 2:10 PM EDT
Posts: 224
Thanks: 143
Thanked 13 Times in 12 Posts
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

Code:
<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)

Code:
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:

Code:
<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?

Moderator's Comments:
Done.
Sponsored Links
    #2  
Old 07-20-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 24 July 2014, 12:54 PM EDT
Location: Galactic Empire
Posts: 3,354
Thanks: 230
Thanked 1,190 Times in 1,123 Posts
Just curious, why don't you simply create a new file rather than replacing?

Code:
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[i] " </td>"
                print "</tr>"

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


Last edited by Yoda; 07-20-2013 at 04:54 PM..
The Following User Says Thank You to Yoda For This Useful Post:
rveri (07-21-2013)
Sponsored Links
    #3  
Old 07-20-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 24 July 2014, 9:47 AM EDT
Location: Aachen, Germany
Posts: 3,871
Thanks: 62
Thanked 919 Times in 872 Posts
This
Code:
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?
The Following User Says Thank You to RudiC For This Useful Post:
rveri (07-21-2013)
    #4  
Old 07-21-2013
rveri rveri is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 11 July 2014, 2:10 PM EDT
Posts: 224
Thanks: 143
Thanked 13 Times in 12 Posts
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 ..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help on inserting data from text file to excel using shell script suman.frnz Shell Programming and Scripting 3 04-24-2013 02:19 AM
C++ inserting data in a file johnbach Programming 2 08-07-2009 12:39 PM
inserting data into a table from a flat file ss_ss Shell Programming and Scripting 1 01-09-2009 08:26 AM
inserting into a data file paul1s UNIX for Dummies Questions & Answers 4 10-13-2006 02:47 AM
Inserting a character in a data file jxh461 Shell Programming and Scripting 2 11-04-2002 04:55 PM



All times are GMT -4. The time now is 12:56 PM.