How to append characters in the beginning and end of file?

I have a list of 200 files, In all the files I want to add the below lines in the beginning and end.

Spool 'file_name.sql' --->  beginning of the file

Spool off --> end of the file.

I want a solution for this both in plsql and unix.

Please help me in finding a solution for this.
sakthikumarAsked:
Who is Participating?
 
johnsoneConnect With a Mentor Senior Oracle DBACommented:
What I would do is this:

#!/bin/sh

for file in *.sql
do
bname=`basename $file .sql`
ex $file << EOF
1
i
spool $bname.out
.
$
a
spool off
.
w
q
EOF
done

Open in new window


This will edit the file in place without using temporary files.  Every file in the directory that ends in .sql will be modified.  If the file is xxx.sql, then the spool command will spool to xxx.out.  The spool command will be on the first line and the spool off command will be on the last line.
0
 
omarfaridCommented:
you can do the following

for file in "put list of files"
do
    echo Spool 'file_name.sql' > $file$$
    cat $file >> $file$$
    echo Spool off >> $file$$
    mv $file $file.$$
    mv $file$$ $file
done

If every thing is fine then you may remove temp files
0
 
sakthikumarAuthor Commented:
file_name is dynamic, need that particular name of the file in each file.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
omarfaridCommented:
How the dynamic file name is specified?
0
 
sakthikumarAuthor Commented:
FOR EG. IF THE FILE NAME IS "KING"
I WANT KING.SQL TO BE APPENDED IN THE BEGINNING OF THE FILE.
0
 
omarfaridCommented:
Try this

for file in "put list of files"
do
    echo "Spool $file.sql" > $file$$
    cat $file >> $file$$
    echo Spool off >> $file$$
    mv $file $file.$$
    mv $file$$ $file
done
0
 
sakthikumarAuthor Commented:
Sorry I am not good in unix

How should I give the list of files in the for loop?

for eg. /home/opctusr1/test

I have all the files that has to be processed in the above directory.

how should I loop it.
0
 
omarfaridCommented:
can you show sample of file names? are they in the same dir? do you have a list of those files stored in a file?
0
 
sakthikumarAuthor Commented:
yes they are in same directory.

see below egs.
RF_FUN_GET_CONSG_CODE.sql
RF_FUN_GET_CONSG_TYPE.sql
0
 
sakthikumarAuthor Commented:
no I dont have the list of files
0
 
omarfaridConnect With a Mentor Commented:
try this

cd /home/opctusr1/test
ls *.sql | while read file
do
    echo "Spool $file.sql" > $file$$
    cat $file >> $file$$
    echo Spool off >> $file$$
    mv $file $file.$$
    mv $file$$ $file
done
0
 
serialbandCommented:
You can do it in a single sed command
for file in * ; do sed -e '1 i\
Spool 'file_name.sql\
' -e '$ a\
Spool off' $file

Open in new window

I left off the -i so that you can see what happens, to be sure you got it right.  You can add the -i option to replace the text directly in the file.  Make sure you copy this exactly as 4 lines with the backslash terminating each line
for file in * ; do sed -i -e '1 i\
Spool 'file_name.sql\
' -e '$ a\
Spool off' $file

Open in new window

0
 
sakthikumarAuthor Commented:
Hi Serial band,
When I execute I am getting the below error.
sh1: Syntax error at line 4 : `'' is not matched.
0
 
serialbandCommented:
Sorry, I cut and pasted your text line from your question, but forgot to escape the single quotes properly.

for file in * ; do sed -i -e '1 i\
Spool '\'file_name.sql\'' \
 -e '$ a\
Spool off' $file; done

Open in new window

Those are all single (') quotes.  There are no double (") quotes.
0
 
johnsoneSenior Oracle DBACommented:
Won't that put the same line in every file?  Every file will contain:

Spool 'file_name.sql'

To me, that is an issue.  Every script will spool to the same file, so every time a script is run it will overwrite the output file.

The other methods posted, either editing in place or using temporary files will not no that.
0
 
serialbandConnect With a Mentor Commented:
Yes, I wasn't sure what he wanted.   I just copied the text from his question.  You'll need to change to double quotes to include the variable and you'll also need to escape the backslash in the first edit command.

for file in *.sql ; do sed -e "1 i\\
Spool $file" \
 -e '$ a\
Spool off' $file; done

Open in new window

Lines 2 & 3 could be merged too, but those backslashes are necessary for the insert and append.
for file in *.sql ; do sed -i -e "1 i\\
Spool $file" -e '$ a\
Spool off' $file; done

Open in new window

 Add the -i option to linux gnu sed to edit the file in place.  Take it out to test the output to the screen.  POSIX compliant Solaris sed doesn't have -i.
0
 
sakthikumarAuthor Commented:
sed: illegal option -- i
Usage: sed [-n] [-e script] [-f source_file] [file...]


I am getting this error when executing. Let me know what changes I need to do.
0
 
serialbandCommented:
That means you don't have gnu sed and just have the old POSIX sed.  You can try to install gnu sed or you can work around that by redirecting them to another file.  Before gnu sed included the -i, you would create a folder to put them in the mv them back, or make a copy and move them back.  You can't redirect to the existing file, because it will wipe it.  I prefer creating a folder for the redirect rather than renaming the file, but you could do either.

mkdir Batch_Copy

for file in *.sql ; do sed  -e "1 i\\
Spool $file" -e '$ a\
Spool off' $file > Batch_Copy\$file; mv Batch_Copy\$file .; done

rmdir Batch_Copy

Open in new window

0
All Courses

From novice to tech pro — start learning today.