Reputation: 75
I am relatively new to linux I want to search a pattern in a file which starts with "Leonard is" and ends on "champion"
Also this pattern might be placed in multiple lines
the input file(input.txt) may look like:
1 rabbit eats carrot Leonard is a champion
2 loin is the king of
3 jungle Leonard is a
4 Champion
5 Leonard is An exemplary
6 Champion
i would want to have all the occurrences of my pattern ignoring all the other characters other than the pattern in the output file:
1 Leonard is a champion
3 Leonard is a
4 Champion
5 Leonard is An exemplary
6 Champion
i have been very close with the following command:
cat input.txt | grep -ioE "Leonard.*Champion$"
as this command only returns
1 Leonard is a champion
ignoring all the patterns occurring in multiple line
if any other approach of searching other than grep is useful kindly let me know Thanks!!
Upvotes: 1
Views: 1382
Reputation: 242373
Perl to the rescue:
perl -l -0777 -e 'print for <> =~ /(.*Leonard(?s:.*?)[Cc]hampion.*)/g' -- input.txt
-l
adds newlines to prints-0777
reads the whole file instead of processing it line by line<>
reads the input.*?
is like .*
, i.e. it matches anything, but the ?
means the shortest possible match is enough. That prevents the regex from matching everything between the first Leonard and last Champion..
in a regex doesn't match a newline normally, but it does with the s
modifier. (?s:.*?)
localizes the changed behaviour, so other dots still don't match newlines.Upvotes: 1
Reputation: 166
The "." is referenced as "any character except new line", therefore, what you're trying to achieve with . is not possible, I suggest using \s with an addition of * or + as well (as suggested above), but need to find out how to implement it with the "grep" reg expression. There are also nice tools for regex testing - https://regexr.com/ for example.
Upvotes: 0
Reputation: 24298
You're looking for \s
which stands for whitespace. +
stands for one or more
Pattern: Leonard is a\s+Champion
See: https://regex101.com/r/qiNXhf/1
I use this tool with 0 knowledge of regex in my mind, and it helps me a lot. See the notes on the right bottom, where all these signs are explained.
Upvotes: 0