How to give a pattern for new line in grep? New line at beginning, new line at end. Not the regular expression way. Something like \n.
相关问题
- JQ: Select when attribute value exists in a bash a
- bash print whole line after splitting line with if
- “command not found” errors in expect script execut
- grep using grep results
- UNIX Bash - Removing double quotes from specific s
相关文章
- Check if directory exists on remote machine with s
- What's the difference between grep -r and -R
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
- Bash script that creates a directory structure
- Test if File/Dir exists over SSH/Sudo in Python/Ba
- How can I create a “tmp” directory with Elastic Be
Thanks to @jarno I know about the -z option and I found out that when using GNU grep with the -P option, matching against
\n
is possible. :)Example:
grep -zoP 'foo\n\K.*'<<<$'foo\nbar'
Prints
bar
grep
patterns are matched against individual lines so there is no way for a pattern to match a newline found in the input.However you can find empty lines like this:
As for the workaround (without using non-portable
-P
), you can temporary replace a new-line character with the different one and change it back, e.g.:Basically it's looking for exact match
_foo_
where_
means\n
(so__
=\n\n
). You don't have to translate it back bytr '_' '\n'
, as each pattern would be printed in the new line anyway, so removing_
is enough.just found
It's using
$'\r'
for c-style escape in Bash.in this article
You can use this way...
-P
is used for Perl regular expressions (an extension to POSIXgrep
).\s
match the white space characters; if followed by*
, it matches an empty line also.^
matches the beginning of the line.$
matches the end of the line.try
pcregrep
instead of regulargrep
:the
-M
option allows it to match across multiple lines, so you can search for newlines as\n
.