Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

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.
0
sakthikumar
Asked:
sakthikumar
  • 7
  • 5
  • 4
  • +1
3 Solutions
 
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
 
omarfaridCommented:
How the dynamic file name is specified?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
johnsoneSenior 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:
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
 
serialbandCommented:
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

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 7
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now