Inserting data - from one file to another?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting data - from one file to another?
# 1  
Old 07-20-2013
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:
Mod Comment Done.
# 2  
Old 07-20-2013
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 05:54 PM..
This User Gave Thanks to Yoda For This Post:
# 3  
Old 07-20-2013
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?
This User Gave Thanks to RudiC For This Post:
# 4  
Old 07-21-2013
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 ..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Inserting data from another source

Hi, I am creating a script using Vi to go out on emails. Each email has is own set of attachments, each with a unique number eg Q12343 and Q67897. I have managed to get the script to put in the first attachment customer number ie Q12343, but can't figure out how to get the second one to come in ie... (4 Replies)
Discussion started by: Cheryl1234
4 Replies

2. UNIX for Dummies Questions & Answers

Inserting shell script input data automatically from a text file

Dear experts, I am new to linux programming. I have a shell script which i should run it on all my samples. I only define input and out put for this script. The inputs are 3 numbers(coordination numbers) which are available in a series of text file. Since i have a lots of samples, it takes a... (5 Replies)
Discussion started by: mohamadreza
5 Replies

3. Shell Programming and Scripting

Need help on inserting data from text file to excel using shell script

Hi, Please help me on this. I want to insert data from text file to excel using shell script nawk -v r=4 -v c=4 -v val=$a -F, 'BEGIN{OFS=","}; NR != r; NR == r {$c = val; print}' "file.csv" I used above one to insert $a value in 4th row, 4th column in an excel file.csv and it... (3 Replies)
Discussion started by: suman.frnz
3 Replies

4. Shell Programming and Scripting

Problem with inserting data

Hi, i am doing a simple script to insert logs into particular table through shell script when i run the script, it is inserting null value. Any explanation on this. Here is my script. #!/bin/sh export db_connection_url=apps/apps@VIS ... (2 Replies)
Discussion started by: Rami Reddy
2 Replies

5. Programming

C++ inserting data in a file

Could anyone help me with an efficient(and easy) way to insert data in a file directly(with out using temp file). example open the file1.txt 11112222 333333 44444444 and insert something say " 99999 " somewhere inside the file as 11112222 333 99999 333 44444444 (2 Replies)
Discussion started by: johnbach
2 Replies

6. Shell Programming and Scripting

inserting data into a table from a flat file

Hi, I want to insert data into a table from a flat file, the file is having around 25 columns and some 10,000 records. The columns values are seperated by a space. Thanks (1 Reply)
Discussion started by: ss_ss
1 Replies

7. Shell Programming and Scripting

Help Inserting data in mysql table

Cant understand the error #!/bin/bash temp="" A="" D=$(date +"%Y-%m-%d") H=$(date +"%R") temp=$(wget -q -O - website | grep -o "Temperature:]**" | grep \-E -o "+") mysql -D "weather_wise" -e "INSERT INTO weather (Date, Hour, Degrees) VALUES ($D,$H, $temp)"; my data types for... (11 Replies)
Discussion started by: vadharah
11 Replies

8. Shell Programming and Scripting

Inserting Lines between data sets using SED?

Hello all and thanks in advance! What I'm looking to do is insert a blank line, anytime the first 9 characters of a given line don't match the first 9 characters of the previous line. i.e. Convert the data set 1 45 64 89 1 89 69 235 2 89 234 67 2 56 90... (1 Reply)
Discussion started by: selkirk
1 Replies

9. UNIX for Dummies Questions & Answers

inserting into a data file

Hello I have a unix variable $HDR in a script, which contains header info, and I need to create it as a new line at the top of a data file which is the input $1 in the script. Paul (4 Replies)
Discussion started by: paul1s
4 Replies

10. Shell Programming and Scripting

Inserting a character in a data file

Can some one tell me how I can insert a "|" (pipe) at the 15th column throughout a file? examples: to insert at begining of line i use :g/^/s//\|/ to insert at ene of line i use :g/$/s//\|/ how can i insert at the 15th column position. Thanks in advance (2 Replies)
Discussion started by: jxh461
2 Replies
Login or Register to Ask a Question