LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-26-2008, 09:22 PM   #1
robbyd
LQ Newbie
 
Registered: Feb 2008
Posts: 2

Rep: Reputation: 0
extracting part of file into another file


I need some help with extracting some data from one file and put in another file.
Basically I have an sql file that has bunch of tables and data. and looks something like this:

CREATE TABLE `table1` ...
(table structure here)
(inserts data here)

CREATE TABLE `table1` ...
(table structure here)
(inserts data here)

CREATE TABLE `table3` ...
(table structure here)
(inserts data here)


and goes on for about 100 tables.. Im trying to figure out out to extract a specific table and put into a separate file.
For example, if i wanted to copy table50 to anther file, what would be the steps to find what line table 50 starts, and what line table 51 starts, as the ending point of table 50, and then put those lines into another file.
Any help would be appreciated. thanks
 
Old 02-26-2008, 09:49 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
If the tables are separated by an empty line, this might work...
Code:
sed -n '/table50/,/^$/p' file.txt >file2.txt
 
Old 02-26-2008, 10:32 PM   #3
robbyd
LQ Newbie
 
Registered: Feb 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks, I was able to get what i needed with that sed command, its new to me.
 
Old 02-26-2008, 11:24 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
You can take a copy in vim and just delete preceding/succeeding lines if it's a one-off.
You can also use a combination of head and tail to do it if you know the line nums.
 
Old 02-26-2008, 11:56 PM   #5
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
You can take a copy in vim and just delete preceding/succeeding lines if it's a one-off.
Trivial note: if I was going to do it in vim, I'd invert that and just write out the relevant section to the new file. e.g., :50,60w newfile
 
Old 02-27-2008, 01:53 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
That too, in Computing in general, there's usually more than one way to do it.
 
Old 02-27-2008, 02:23 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Yet another way in vim is to use:
1G
:/table50/,/^$/w extract

The "1G" just moves the cursor before the table (by going to line 1), so it may not be necessary. If you need to look at the sql script to determine what the table is called anyway, you don't need to exit vim (or view) to do the extraction.

Sed and Vim are both related to "ed".

Last edited by jschiwal; 02-27-2008 at 02:27 AM.
 
  


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
Extracting a file from an image MS3FGX Linux - Software 6 01-14-2008 02:07 PM
editing a very large HTML file (or, extracting URLs from a file) Chriswaterguy Linux - Software 3 11-27-2007 07:07 PM
Bash remove part of a file based on contents of another file bhepdogg Programming 4 01-31-2007 04:13 PM
extracting gz file..... b123coder Linux - Newbie 1 11-21-2004 08:55 AM
extracting this file... Xylicon Linux - General 4 01-23-2003 09:49 AM


All times are GMT -5. The time now is 07:23 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration