BEFORE APPEND | Database Journal

BEFORE APPEND

Sep 11, 2008
1 minute read

>>Script Language and Platform: Oracle

This script will insert a message into a specific line of multiple files.

I have the following files in a directory.

 [oracle@localhost jp]$ ls -l
 total 48
 -rwxrwxrwx 1 oracle oinstall 258 Aug 10 22:56 jp1.dat
 -rwxrwxrwx 1 oracle oinstall 258 Aug 10 22:56 jp2.dat
 -rwxrwxrwx 1 oracle oinstall 258 Aug 10 22:56 jp3.dat
 -rwxrwxrwx 1 oracle oinstall 258 Aug 10 22:56 jp4.dat
 -rwxrwxrwx 1 oracle oinstall 258 Aug 10 22:57 jp5.dat

I have to include the following message
“THIS SCRIPT BELONGS TO ROAD FARMS COMPANY”
in the third line of each of these files.

The redirection option “>>” can append the line to
the end of a file. Let us see how to insert the message
in the middle of a file.

I checked whether any of my files contain the message.

 [oracle@localhost jp]$ grep “THIS SCRIPT BELONGS TO ROAD FARMS  COMPANY” *dat
 [oracle@localhost jp]$

I created a while loop to implement the task of copying
the message in the middle of the files *.dat.

 [oracle@localhost jp]$ cat while_loop.sh
 #!/bin/sh -x
 #Author JP Vijaykumar Oracle DBA
 #Date   08 – 08 – 08
 #THIS SCRIPT INSERTS THE FOLLOWING MESSAGE
 #IN THE THIRD LINE OF FILES *.DAT
 #THIS SCRIPT BELONGS TO ROAD FARMS  COMPANY
 ls -1 |grep -v while_loop.sh|while read FILE
 do
 head -2 $FILE >> TEMP
 echo “THIS SCRIPT BELONGS TO ROAD FARMS  COMPANY” >> TEMP
 tail -`cat $FILE|wc -l|awk ‘{print $0 -2}’` $FILE >> TEMP
 mv TEMP $FILE
 done
 exit

I executed the script.

 [oracle@localhost jp]$./while_loop.sh

I checked whether any of my files contain this message.

 [oracle@localhost jp]$ grep “THIS SCRIPT BELONGS TO ROAD FARMS COMPANY” *dat
 jp1.dat:THIS SCRIPT BELONGS TO ROAD FARMS COMPANY
 jp2.dat:THIS SCRIPT BELONGS TO ROAD FARMS COMPANY
 jp3.dat:THIS SCRIPT BELONGS TO ROAD FARMS COMPANY
 jp4.dat:THIS SCRIPT BELONGS TO ROAD FARMS COMPANY
 jp5.dat:THIS SCRIPT BELONGS TO ROAD FARMS COMPANY

Please test this script thoroughly before implementing.

Author: JP Vijaykumar



Disclaimer:
We hope that the information on these script pages is
valuable to you. Your use of the information contained in these pages,
however, is at your sole risk. All information on these pages is provided
“as -is”, without any warranty, whether express or implied, of its accuracy,
completeness, or fitness for a particular purpose…

Disclaimer Continued

Back to Database Journal Home

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.