1. To select blocks of text and select only the first block (match)
sed -n '/PATTERN START/,/PATTERN END/p'
-n suppress multiple prints
or
sed ''/PATTERN START/,/PATTERN END/!d'
had the same effect. It deletes all lines which do not fall under this REGEXP matching pattern.
But this will select ALL blocks which match. Worse if there is PATTERN START and no PATTERN END then sed will go to the EOF and print everything. This is something we do not want.
PATTERN START
......
......
PATTERN END
PATTERN START
......
......
EOF
so we have to make it quit after matching the first block.
sed ''/PATTERN START/,/PATTERN END/!d;/PATTERN END/q'
; ends the first command and sed starts executing the next set of commands which in this case is match pattern /PATTERN END/ and the command for sed is to quit on encountering this pattern which happens after matching the first block of code.
Also see: http://www.catonmat.net/blog/sed-one-liners-explained-part-three/
sed -n '/PATTERN START/,/PATTERN END/p'
-n suppress multiple prints
or
sed ''/PATTERN START/,/PATTERN END/!d'
had the same effect. It deletes all lines which do not fall under this REGEXP matching pattern.
But this will select ALL blocks which match. Worse if there is PATTERN START and no PATTERN END then sed will go to the EOF and print everything. This is something we do not want.
PATTERN START
......
......
PATTERN END
PATTERN START
......
......
EOF
so we have to make it quit after matching the first block.
sed ''/PATTERN START/,/PATTERN END/!d;/PATTERN END/q'
; ends the first command and sed starts executing the next set of commands which in this case is match pattern /PATTERN END/ and the command for sed is to quit on encountering this pattern which happens after matching the first block of code.
Also see: http://www.catonmat.net/blog/sed-one-liners-explained-part-three/
Comments