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.

link|improve this question

67% accept rate
are ; characters followed by new line? do you want whole lines or just the part with CREATE..; ? – Can Kavaklıoğlu Dec 27 '11 at 11:10
all line in between CREATE..; including the line having CREATE and ; – Aha Dec 27 '11 at 11:13
feedback

2 Answers

up vote 4 down vote accepted

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 );
link|improve this answer
feedback

Try this

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

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.