LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-18-2012, 09:35 AM   #1
mohan.jiggy
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Rep: Reputation: Disabled
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.
 
Old 04-18-2012, 10:30 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by mohan.jiggy View Post
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
 
Old 04-18-2012, 10:57 AM   #3
mohan.jiggy
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
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 ,
 
Old 04-18-2012, 12:32 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Not too tricky in awk:
Code:
awk 'BEGIN{i=1}/^[A-Z].*proc/,/\//{print > "File"i}/\//{i++}' orig_file
 
Old 04-18-2012, 03:24 PM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
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!
 
Old 10-15-2012, 08:46 AM   #6
mohan.jiggy
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks:)

Thank you all for your solution
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read from binary file and save it as KML file in C dexter910 Programming 1 01-17-2012 02:01 PM
awk: how to read dan manipulate data in multi files from a file mauludi Linux - Newbie 4 05-27-2011 01:52 AM
how can i get my scripts to read/write text from/to specific parts of files? Siljrath Linux - General 12 02-06-2010 02:58 PM
Shell programming: Question about source files and read data from the files ccwq Programming 3 08-05-2007 12:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration