Shell script to perform appending of .sql files

Hi, We are having multiple .sql files generated programatically which is not containing..

create or replace

-- at the start
and

/

-- at the end

We need to append those .sql files with the above 2 statements at their respective positions.

We will be really thankful to get responses regarding shell script to perform the same.

Do you need it on all the lines of the *.sql or only at the start and end of the file ?
For all the lines, here is the solution

sed -e 's/^/create /g' -e 's/$/ \//g' file  > new-file
1 Like

Try below.

 
for file in *.sql ; do
awk 'BEGIN{printf "create or replace "}{print $0}END{print "/"}' $file > temp
mv temp $file
done
1 Like

make sure you support your local charities ...

echo "create or replace" > /tmp/sqlfile
cat yourfile.sql >> /tmp/sqlfile
echo "/" >> /tmp/sqlfile
mv /tmp/sqlfile yourfile.sql
1 Like

You could use a grouping command form:

for file in *.sql
do
        {
                echo "create or replace"
                cat "$file"
                echo "/"
        } # > tmp
        # mv tmp "$file"
done

Remove the hash sign # if the output looks good to redirect the output to tmp file and rename it back to original.

1 Like

Thank you so much for all your help I really appreciate it.

Thank you for helping out