How to read a data from a file as parts and save in different files
Hi gems,
Here is my query. I have a exported sql file with 20 procedures , Now i want to read each seperate procudure in to a seperate file .(totally 20 files for 20 procedures ) please can you guide me , how can i do this using "shell script ". Thanks in advance. |
Quote:
Daniel B. Martin |
My Exported sql file is like this
================================= prompt prompt Creating procedure proc1 prompt ===================================== prompt CREATE OR REPLACE PROCEDURE proc1 ----- -------- END p_Wf_proc1; / prompt prompt Creating procedure proc2 prompt ===================================== prompt CREATE OR REPLACE PROCEDURE proc2 ----- -------- END p_Wf_proc2; / prompt prompt Creating procedure proc3 prompt ===================================== prompt CREATE OR REPLACE PROCEDURE proc3 ----- -------- END p_Wf_proc3; / I need this to be in 3 different files: File1: ===== CREATE OR REPLACE PROCEDURE proc1 ----- -------- END p_Wf_proc1; / File2: ===== CREATE OR REPLACE PROCEDURE proc2 ----- -------- END p_Wf_proc2; / File3: ===== CREATE OR REPLACE PROCEDURE proc3 ----- -------- END p_Wf_proc3; / Your solution will be helpful for me , |
Not too tricky in awk:
Code:
awk 'BEGIN{i=1}/^[A-Z].*proc/,/\//{print > "File"i}/\//{i++}' orig_file |
If you really need to do it using Bash or a POSIX shell,
Code:
#!/bin/sh The way this one works is: The first if..fi clause checks if there is at least one parameter, the output base filename. If there are no parameters, or only -h or --help is given, the terse usage is printed, and the script aborts. The first parameter is saved in BASE, and removed from the positional parameters using shift 1 . This way "$@" will expand to the rest of the parameters given to the script, but not include BASE. The cat command outputs all specified input files, or if none, the standard input. This is one of the rare valid uses for cat before a pipe. The output is redirected to a subshell (the bit in parentheses). The subshell uses exec >filename to redirect future output by any command in that subshell to a file. The while loop reads each input line into LINE. It looks a bit weird because this way it works right even if the final line does not contain a newline. (Clearing the LINE to an empty string is part of that trick.) The loop body just echoes the line just read. Because we've redirected output, it ends up in some file. If the line contains only a slash, the nested if clause within the loop body checks if the file is not empty ([ -s filename ] is true only if the file contains something). If it contains anything, we increase the count, and redirect output to a new file. This extra check is to avoid you getting extra empty files. Finally, the subshell redirects output to a black hole (/dev/null, goes nowhere!), and checks if the last file we've redirected output to is empty. If it is empty, we remove it. Again, this is to just avoid getting extra empty files. That's about it. Note, however, that I haven't tested the above, just written it blindly; it might have bugs in it. You must check it before use! |
Thanks:)
Thank you all for your solution
|
All times are GMT -5. The time now is 03:24 AM. |