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.
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
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
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>
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