BEFORE APPEND

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

Get the Free Newsletter!

Subscribe to Cloud Insider for top news, trends & analysis

Latest Articles