LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   extracting part of file into another file (https://www.linuxquestions.org/questions/linux-newbie-8/extracting-part-of-file-into-another-file-624079/)

robbyd 02-26-2008 08:22 PM

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

homey 02-26-2008 08:49 PM

If the tables are separated by an empty line, this might work...
Code:

sed -n '/table50/,/^$/p' file.txt >file2.txt

robbyd 02-26-2008 09:32 PM

Thanks, I was able to get what i needed with that sed command, its new to me.

chrism01 02-26-2008 10:24 PM

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.

slakmagik 02-26-2008 10:56 PM

Quote:

Originally Posted by chrism01 (Post 3071041)
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

chrism01 02-27-2008 12:53 AM

That too, in Computing in general, there's usually more than one way to do it.

jschiwal 02-27-2008 01:23 AM

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".


All times are GMT -5. The time now is 02:19 PM.