Home > linux > grep | About

Print only the necessary part

Sometimes you need only a small portion of the line, not the whole line. Consider this list of files

I only need the part that is between the M and the P

Using the -o switch

gives me exactly what I want

Greedy regexes

Now suppose I need the part that's between the P and the first dot ".", right before the host name

Using .* will eat as much characters as possible so it will glob all characters until the last dot, not the first. the .*? syntax (used in Perl and Python for example) is a greedy syntax, meaning it will match the smallest number of characters, not the biggest.

The .*? gave no results because by default grep doesn't support the greedy expressions, we need to enable them with -P (Perl regexes)



contact : @ychaouche yacinechaouche at yahoocom


QR Code
QR Code Print only the necessary part (generated for current page)