Publicado 2019-03-11. | Modificado 2020-07-11.
Palabras clave: grep
Utilizar varias expresiones en la misma linea mediante el parámetro -e
.
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. If this option is used multiple times or is combined with the -f (--file) option, search for all patterns given. This option can be used to protect a pattern beginning with “-”.
Ejemplo: Eliminar comentarios y lineas en blanco, generalmente para inspeccionar archivos de configuración:
grep -v -e '^#' -e '^\s*$' /etc/adduser.conf
Obtener texto a partir de un patrón ignorando un prefijo mediante el
parámetro -o
y el uso de \K
en una expresión de tipo PCRE:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
-P, --perl-regexp
Interpret the pattern as a Perl-compatible regular expression (PCRE). This is experimental and grep -P may warn of unimplemented features.
MATCH POINT RESET
\K reset start of match
\K is honoured in positive assertions, but ignored in negative ones.
Ejemplo: Obtener el tipo de filesystem de /
:
grep -oP '^[^#]+\s+/\s+\K\S+' /etc/fstab
Obtener texto a partir de un patrón ignorando prefijo y sufijo usando -o
en una expresión de tipo PCRE con -P
:
"(?<=pattern)"
A zero-width positive lookbehind assertion. For example, "/(?<=\t)\w+/" matches a word that follows a tab, without including the tab in $&.
"(?=pattern)"
A zero-width positive lookahead assertion. For example, "/\w+(?=\t)/" matches a word followed by a tab, without including the tab in $&
Ver: https://unix.stackexchange.com/questions/13466/can-grep-output-only-specified-groupings-that-match