Script to insert text from one file to other file

Hello Team,

I need help in preparing script which will insert text from one file to other file.

I have requirement to prepare script which will insert data from one file to another file.

I have tried using sed and awk command but it is not useful to me as it does not append data in the exixting file. If we try to do that,it just emmpty the file.

kindly provide help in perparing the script.

Hi,

Post your input and expected output.

Actually we have to frequently restart our weblogic application server. After restart we have to update a operational page which we do it manually as of now. I would like to automate this task. The line which we insert in operational page is having below format.

<span class="text"> 2010-07-15 9:19:52 AM CEST MARS Server server_name was restarted due to outofmemory.<br></span>

My requirement of script is to extract data and time from the file and insert those data in a file which is having above data and fromthere that data should get inserted in actual operational file. The data should always get inserted after the following line in a operational file.

</font> </b></font></span><p>

Please do let me know in case of any other queries

echo "<span class=\"text\"> `date` CEST MARS Server `hostname` was restarted due to outofmemory. <br></span>" >> operational_page 

Hello rdcwayx,

Thanks for the reply.

But the command given by you will append the line in a operational status file.

I would like to append that line after following line in a status file.

</font> </b></font></span><p>

The new text should always get appended after the following line in a operational file.

</font> </b></font></span><p>
d=$(date)   # or you get the date and time from log files.
h=$(hostname)

 awk -v a="$d" -v b="$h"  '/<\/font> <\/b><\/font><\/span><p>/ 
            {$0=$0"\n<span class=\"text\"> " a " CEST MARS Server " b " was restarted due to outofmemory. <br></span>"}1' operational_page

Hello rdcwayx,

Thanks for the reply.

I have exceuted the command given by you but it's giving following error.

awk: syntax error near line 1
awk: bailing out near line 1

If on Solaris, use /usr/bin/nawk or /usr/xpg4/bin/awk

The solution to give full path of awk command is working fine.

But the output only get displayed on the screen and not adding it in the file.

I would like the line should get added after the below line in a operational file.

</font> </b></font></span><p>
bash-3.00$ /usr/xpg4/bin/awk -v a="$d" -v b="$h"  '/<\/font> <\/b><\/font><\/span><p>/
{$0=$0"\n<span class=\"text\"> " a " CEST Server " b " was restarted due to outofmemory. <br></span>"}1' abc.html >abc1.html

If i redirect output to same abc.html file,the file becomes empty and if i redirect it to some other file suppose abc1.html,the file contains following output. I would like the same operational file should get aapended with the following line.

<span class="text"> Sat Jul 17 11:17:34 CEST 2010 CEST Server  was restarted due to outofmemory. <br></span>

Output Of abc1.html

</font> </b></font></span><p>
</font> </b></font></span><p>
<span class="text"> Sat Jul 17 11:17:34 CEST 2010 CEST MARS Server weba1-mars was restarted due to outofmemory. <br></span>

By awk, you have to redirect the output to a temp file, and rename it back.

Hello rdcwayx,

Thanks for the update,

My requirement is the text which i am going to update in operational file will be in some file xyz. Suppose abc file conatins this text

(<span class="text"> Sat Jul 17 11:17:34 CEST 2010 CEST Server was restarted due to outofmemory. <br></span>)

The above text from "abc" file should get appended after a line ( </font> </b></font></span><p> ) in "xyz" file.

can you help me to do this using script?

Try and adapt the following script :

awk '
BEGIN {
   itext   = "Server was restarted due to outofmemory.";
   itextRe = "span class=\"text\">[^<>]*" text "[[:space:]]*<br></span>";
   otextRe = "</font> </b></font></span><p>";
}
NR==FNR {
   if (match($0,itextRe))
      out = substr($0, RSTART, RLENGTH);
   next;
}
1
$0 ~ otextRe {
   if (out) print out;
   out = "";
}
' cool.abc cool.xyz

Input file abc

<span class="text">aaaa</span>
sssssssssssss
<span class="text"> Sat Jul 17 11:17:34 CEST 2010 CEST Server was restarted due to outofmemory. <br></span>
<span class="text">end</span>

Input file wyz:

text text text text
</font> </b></font></span><p>
another text another text another text
</font> </b></font></span><p>
blablablabla

Output:

text text text text
</font> </b></font></span><p>
span class="text"> Sat Jul 17 11:17:34 CEST 2010 CEST Server was restarted due to outofmemory. <br></span>
another text another text another text
</font> </b></font></span><p>
blablablabla

Jean-Pierre.

S=$(grep "Server was restarted due to outofmemory" abc)

awk -v a="$S" '/<\/font> <\/b><\/font><\/span><p>/ {$0=$0"\n" a}1' xyz