Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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

2 Answers 2

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

Try this

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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