Solved

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

Posted on 2014-03-22
18
490 Views
Last Modified: 2014-04-03
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.
0
Comment
Question by:sakthikumar
  • 7
  • 5
  • 4
  • +1
18 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 39948245
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
 

Author Comment

by:sakthikumar
ID: 39948307
file_name is dynamic, need that particular name of the file in each file.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39948309
How the dynamic file name is specified?
0
 

Author Comment

by:sakthikumar
ID: 39948339
FOR EG. IF THE FILE NAME IS "KING"
I WANT KING.SQL TO BE APPENDED IN THE BEGINNING OF THE FILE.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39948340
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
 

Author Comment

by:sakthikumar
ID: 39948434
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 39948439
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
 

Author Comment

by:sakthikumar
ID: 39948448
yes they are in same directory.

see below egs.
RF_FUN_GET_CONSG_CODE.sql
RF_FUN_GET_CONSG_TYPE.sql
0
 

Author Comment

by:sakthikumar
ID: 39948449
no I dont have the list of files
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 34

Accepted Solution

by:
johnsone earned 300 total points
ID: 39948483
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
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 100 total points
ID: 39948719
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
 
LVL 27

Expert Comment

by:serialband
ID: 39957597
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
 

Author Comment

by:sakthikumar
ID: 39968736
Hi Serial band,
When I execute I am getting the below error.
sh1: Syntax error at line 4 : `'' is not matched.
0
 
LVL 27

Expert Comment

by:serialband
ID: 39969455
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
 
LVL 34

Expert Comment

by:johnsone
ID: 39969476
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
 
LVL 27

Assisted Solution

by:serialband
serialband earned 100 total points
ID: 39969759
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
 

Author Comment

by:sakthikumar
ID: 39974099
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
 
LVL 27

Expert Comment

by:serialband
ID: 39974167
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now