0

How to fetch lines between two strings using shell script? For example I am having a file called file_text.txt as below.

MEDIUMINT
CREATE ( 
MAXVALUE 
MEDIUMBLOB
MEDIUMINT
MEDIUMTEXT
MIDDLEINT );
MINUTE_SECOND
MINUTE_SECOND
CREATE (
MINUTE_MICROSECOND
MINUTE_SECOND
MOD );
MODIFIES
MEDIUMINT
CREATE (
NATURAL
NOT
NO_WRITE_TO_BINLOG
NULL );
NUMERIC
ON
OPTIMIZE

I want to grep all lines between CREATE and ; from the file. Including the CREATE and ;string.

2
  • are ; characters followed by new line? do you want whole lines or just the part with CREATE..; ? Dec 27, 2011 at 11:10
  • all line in between CREATE..; including the line having CREATE and ;
    – Aha
    Dec 27, 2011 at 11:13

2 Answers 2

4

Something like that:

sed '/^CREATE/,/);$/!d;s/^CREATE (//;s/);$//' file_text.txt

will output:

MAXVALUE 
MEDIUMBLOB
MEDIUMINT
MEDIUMTEXT
MIDDLEINT 

MINUTE_MICROSECOND
MINUTE_SECOND
MOD 

NATURAL
NOT
NO_WRITE_TO_BINLOG
NULL 

Or in case you need CREATE ( and ); just do

sed '/^CREATE/,/);$/!d' file_text.txt

It will do smth like that:

CREATE ( 
MAXVALUE 
MEDIUMBLOB
MEDIUMINT
MEDIUMTEXT
MIDDLEINT );
CREATE (
MINUTE_MICROSECOND
MINUTE_SECOND
MOD );
CREATE (
NATURAL
NOT
NO_WRITE_TO_BINLOG
NULL );
0

Try this

 echo "...youriput..." | tr -d "\n" | tr ';' '\n' | grep CREATE | sed "s/.*CREATE\(.*\)/CREATE\1;/"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .