Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: Extract Range with Regular Expressioin (maybe sed?)

Tags:

regex

bash

unix

sed

I have a file that is similar to this:

<many lines of stuff>
SUMMARY:
<some lines of stuff>
END OF SUMMARY

I want to extract just the stuff between SUMMARY and END OF SUMMARY. I suspect I can do this with sed but I am not sure how. I know I can modify the stuff in between with this:

sed "/SUMMARY/,/END OF SUMMARY/ s/replace/with/" fileName

(But not sure how to just extract that stuff).

I am Bash on Solaris.

like image 487
sixtyfootersdude Avatar asked Apr 12 '10 14:04

sixtyfootersdude


3 Answers

sed -n "/SUMMARY/,/END OF SUMMARY/p" fileName
like image 79
sixtyfootersdude Avatar answered Sep 29 '22 18:09

sixtyfootersdude


If Perl is fine you can use:

perl -e 'print $1 if(`cat FILE_NAME`=~/SUMMARY:\n(.*?)END OF SUMMARY/s);'
like image 30
codaddict Avatar answered Sep 29 '22 20:09

codaddict


If you don't want to print the marker lines:

sed '1,/SUMMARY/d;/END OF SUMMARY/,$d' filename
like image 32
Dennis Williamson Avatar answered Sep 29 '22 20:09

Dennis Williamson