3 ms·
A use-case for lookarounds that I often use is: grep -Po '(?<=...)pattern' Which also cuts out and prints the relevant part of the line. This saves a trip
by sshine 9y ago
A use-case for lookarounds that I often use is:
grep -Po '(?<=...)pattern'
Which also cuts out and prints the relevant part of the line. This saves a trip through cut, awk or perl. (-P is PCRE and -o is print only matched characters, which the lookarounds aren't a part of.)
- asicsp 9y agoI often use `\K` instead, which also helps if it is variable length lookbehind $ echo 'foo=5, Bar=3; x1=83, y=120' | grep -oP '\b[a-z]+=\K\d+' 5 120 further reading: https://stackoverflow.com/questions/11640447/variable-length-lookbehind-assertion-alternatives-for-regular-expressions/11640500#11640500 https://stackoverflow.com/questions/11640447/variable-length...
- JepZ 9y agoI like to add the lookahead: grep -oP '(?<=...)pattern(?=...)' The caveat however is that the look{ahead,behind} pattern has to be of fixed length.
- mnarayan01 9y agoWow. I do this all the time, and the additional step of piping to sed never ceased to annoy me. You've made my day.