Sunday, November 2, 2014

sed one liners explained

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 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.

No comments:

OK GOOGLE on Samsung Galaxy S7 doesn’t work

To make Ok Google detection work on Galaxy S7 (Galaxy series phones) we need to perform a couple of steps. 1. As long as Samsung S vo...