user3299633
user3299633

Reputation: 3380

Extract text into new file

I have a text file with the following format

CREATE TABLE `a` (
.
.
);

CREATE TABLE `b` (
.
.
);

and so on and so on...

I want to extract individual tables and am trying with sed but it doesn't seem to work:

sed -n '/^CREATE\sTABLE\s`a`/,/^;$/p' file

This is unfortunately not providing the intended behavior. It is producing the entirety of the file instead of just the code block.

It should only grab one code block.

Found issue (dumb typo):

sed -n '/^CREATE\sTABLE\s`a`/,/;$/p' file

Upvotes: 0

Views: 61

Answers (2)

With sed and suppressing the last line by head -n -1

# sed -ne '/CREATE TABLE `b`/,/CREATE TABLE/p' tmp7|head -n -1
CREATE TABLE `b` (
.
.
);

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203254

$ awk -v RS= '$3=="`a`"' file
CREATE TABLE `a` (
.
.
);

$ awk -v RS= '$3=="`b`"' file
CREATE TABLE `b` (
.
.
);

If that's not what you need then edit your question to provide more truly representative sample input and the associated expected output.

Upvotes: 1

Related Questions