Script to insert text from one file to other file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to insert text from one file to other file
# 8  
Old 07-17-2010
If on Solaris, use /usr/bin/nawk or /usr/xpg4/bin/awk
# 9  
Old 07-17-2010
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.
Code:
</font> </b></font></span><p>

Code:
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.
Code:
<span class="text"> Sat Jul 17 11:17:34 CEST 2010 CEST Server  was restarted due to outofmemory. <br></span>

Output Of abc1.html
Code:
</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>


Last edited by Scott; 11-26-2010 at 06:58 AM.. Reason: Code tags
# 10  
Old 07-20-2010
By awk, you have to redirect the output to a temp file, and rename it back.
# 11  
Old 07-21-2010
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
Code:
(<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?

Last edited by Scott; 11-26-2010 at 06:59 AM.. Reason: Code tags
# 12  
Old 07-21-2010
Try and adapt the following script :
Code:
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
Code:
<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:
Code:
text text text text
</font> </b></font></span><p>
another text another text another text
</font> </b></font></span><p>
blablablabla

Output:
Code:
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.
# 13  
Old 07-21-2010
Code:
S=$(grep "Server was restarted due to outofmemory" abc)

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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

TCL script to insert some text on a file

Hi All , I am looking to create one TCL script to insert one text based on some regular expression match on one file as stated below Input File module (mem1 ,mem2 , bist1 , ten2 , sen1 , land2 , taane2 , ran1 , ran2 , tri2 , tri8 , fly1 , fly2 , san2 ); output ran1 , ran2 , tri2 ,... (1 Reply)
Discussion started by: kshitij
1 Replies

2. Shell Programming and Scripting

How to insert text within a file?

Hi, I am trying to check for missing dates in a file and would want to insert the missing date into the file. Currently the script is as below #!/bin/ksh dates="dates" cat ${dates} | grep -v "^#" curr_month=`date '+%m` curr_day=`date '+%d` curr_year=`date '+%Y` #curr_month=02... (7 Replies)
Discussion started by: newbie_01
7 Replies

3. UNIX for Dummies Questions & Answers

Insert text into a file using shell script

Hi, I need to insert "Hello World" text into a file called hai.txt using shell scripting. Kindly help me. For eg: If I open the file hai.txt by giving linux command cat hai.txt, the content of the file should have the text Hello World in it. Thanks (5 Replies)
Discussion started by: karthick nath
5 Replies

4. Shell Programming and Scripting

Insert value to db from text file

Hi, I have a single value in insertval file. I want to load that value to database with the current date. I tried the below code but it is inserting <NULL> to database and echo $c is also null. cat insertval | awk -F ' ' '{print $1}' > c echo c=$c data=`sqlplus -s user/pwd@hostname <<EOF ... (5 Replies)
Discussion started by: Neethu
5 Replies

5. Shell Programming and Scripting

Insert Text On file

Hi All, Can someone pls help me to insert some text on a file. my file contains something like below.. AKBULBU, BALUMIL, BATCH,BATCH BOARROB, BOTAKAT, C57896, CAKIOZE, CHECMER, CICOFRA, CISZPAW,2194485 I want output as USER_ID, LOGIN_ID (6 Replies)
Discussion started by: harshakusam
6 Replies

6. Shell Programming and Scripting

awk script to insert text in file

I'm in a time crunch here and I don't know how to write scripts. I have a file of data that looks like the following: 1 7.652073E+00 0.000000E+00 7.146691E+02 1.704154E+01 2 7.652068E+00 6.031387E+00 7.146636E+02 2.499305E+01 3 7.645906E+00 1.509455E+01 7.144158E+02 1.822061E+02 4... (7 Replies)
Discussion started by: pk218703
7 Replies

7. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

8. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

9. Shell Programming and Scripting

How to insert text into first line of the file and middle of the file?

Script 1 Pre-requisites Create a file with x amount of lines in it, the content of your choice. Write a script that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the very top (the... (3 Replies)
Discussion started by: ali hussain
3 Replies
Login or Register to Ask a Question