Search code examples
unixaix

Extracting a multiple lines from a file in AIX


I have a requirement where I need to fetch multiple lines from a file in AIX server based on a input string.

For e.g. If I have below statement inside a file. I want to fetch the lines which starts from INSERT and should keep fetching the lines till I encounter first semicolon.

select date from student ;

insert into abc
select * from abc;

So my output should look like as shown below:

insert into abc
select * from abc;

Solution

  • You can use sed to do the job for you

    sed '/<pattern1>/,/<pattern2>/!d;/<pattern2>/q'
    

    Here d is for deleting the lines which do !not fall within the range, then q quitting the first time it encounters the end of the range.

    sed '/insert/,/;/!d;/select/q' test.txt
    

    or

    sed '/insert/,/;/!d;/;/q' test.txt
    

    o/p -

    insert into abc
    select * from abc;