Link to home
Start Free TrialLog in
Avatar of sakthikumar
sakthikumar

asked on

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.
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

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
Avatar of sakthikumar
sakthikumar

ASKER

file_name is dynamic, need that particular name of the file in each file.
How the dynamic file name is specified?
FOR EG. IF THE FILE NAME IS "KING"
I WANT KING.SQL TO BE APPENDED IN THE BEGINNING OF THE FILE.
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
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.
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?
yes they are in same directory.

see below egs.
RF_FUN_GET_CONSG_CODE.sql
RF_FUN_GET_CONSG_TYPE.sql
no I dont have the list of files
ASKER CERTIFIED SOLUTION
Avatar of johnsone
johnsone
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Hi Serial band,
When I execute I am getting the below error.
sh1: Syntax error at line 4 : `'' is not matched.
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.
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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