Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-03-22
18
Medium Priority
?
502 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 

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

Accepted Solution

by:
johnsone earned 1200 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 400 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 30

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 30

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 35

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 30

Assisted Solution

by:serialband
serialband earned 400 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 30

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Suggested Courses

719 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