Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

SQL Scripts & Samples

Posted Sep 11, 2008

BEFORE APPEND

By DatabaseJournal.com Staff



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



SQL Scripts & Samples Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
SQL Scripts & Samples Forum
Topic By Replies Updated
sql help!! add 1 August 22nd, 11:58 AM
How To Combine These 2 Queries into 1 Query ? tarek_land 1 June 7th, 08:37 AM
solving query svibuk 1 February 3rd, 06:08 AM
converting from a character string to uniqueidentifier saturnius 4 January 4th, 05:56 AM


















Thanks for your registration, follow us on our social networks to keep up-to-date