LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to read a data from a file as parts and save in different files (https://www.linuxquestions.org/questions/programming-9/how-to-read-a-data-from-a-file-as-parts-and-save-in-different-files-940465/)

mohan.jiggy 04-18-2012 09:35 AM

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.

danielbmartin 04-18-2012 10:30 AM

Quote:

Originally Posted by mohan.jiggy (Post 4656199)
Now i want to read each separate procedure in to a separate file.

Help us to help you. You described your problem. Construct sample input and output files and post them here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.

Daniel B. Martin

mohan.jiggy 04-18-2012 10:57 AM

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 ,

grail 04-18-2012 12:32 PM

Not too tricky in awk:
Code:

awk 'BEGIN{i=1}/^[A-Z].*proc/,/\//{print > "File"i}/\//{i++}' orig_file

Nominal Animal 04-18-2012 03:24 PM

If you really need to do it using Bash or a POSIX shell,
Code:

#!/bin/sh

if [ $# -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  echo ""
  echo "Usage: $0 [ -h | --help ]"
  echo "      $0 OUTPUT [ INPUT(s)... ]"
  echo ""
  echo "This will split the input after lines containing only"
  echo "a slash (/) into separate files. The first output file"
  echo "will be OUTPUT.1, the second OUTPUT.2, and so on."
  echo "The slash will be the last line in each output file"
  echo "(except possibly the last file, if there is no trailing slash)."
  echo ""
  exit 1
fi

BASE="$1"
shift 1

cat "$@" | (
  COUNT=1
  exec >"$BASE$COUNT"

  LINE=''
  while read -r LINE || [ -n "$LINE" ]; do
      echo "$LINE"
      if [ "$LINE" = "/" ]; then
          if [ -s "$BASE$COUNT" ]; then
              COUNT=$[COUNT + 1]
              exec >"$BASE$COUNT"
          fi
      fi
      LINE=''
  done

  exec >/dev/null
  [ -s "$BASE$COUNT" ] || rm -f "$BASE$COUNT"
)

Save in a file, make it executable, and run it without parameters to see the usage: The first parameter is the base name to which the proc number is appended to get the output file names, and the rest of the parameters are input file names. If there are no input file names, it expects input from standard input, so you can use it as a pipe too.

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!

mohan.jiggy 10-15-2012 08:46 AM

Thanks:)
 
Thank you all for your solution


All times are GMT -5. The time now is 03:24 AM.