Open a Terminal by pressing Ctrl+Alt+T and use the one of below one linear command. Don't forget to change the input filename to yours.
Using awk:
awk -v FS="(STRING1|STRING2)" '{print $2}' inputfile > outputfile
Using grep:
grep -Pzo "(?<=STRING1)(.|\n)*(?=STRING2)" inputfile > outputfile
or with DOTALL (Dot Matches Line Breaks) modifier. It makes .
to match even \n
ew line characters.
grep -Pzo "(?s)(?<=STRING1).*?(?=STRING2)" inputfile > outpuffile
The (?s)
actives the DOTALL for grep.
or as another alternative to match \n
ew line chars, simply use:
grep -Pzo "(?<=STRING1)[\s\S]*(?=STRING2)" inputfile > outpuffile
In man grep:
-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 PATTERN as a Perl compatible regular expression (PCRE)
-z, --null-data
Treat the input as a set of lines, each terminated by a zero byte (the ASCII
NUL character) instead of a newline. Like the -Z or --null option, this option
can be used with commands like sort -z to process arbitrary file names.
(?<=pattern)
: Known as Positive Lookbehind. A pair of parentheses, with the opening parenthesis followed by a question mark, "less than" symbol, and an equals sign.
So, the (?<=STRING1).*?
(positive lookbehind) matches the 0 or more occurrences of any characters(which are optional because of using ?
after .*
) followed by STRING1
from inputfile.
(?=pattern)
: Known as Positive Lookahead: The positive lookahead construct is a pair of parentheses, with the opening parenthesis followed by a question mark and an equals sign.
So, the .*?(?=STRING2)
: (positive lookahead) matches 0 or more occurrences of any characters followed by STRING2
.
Links for reading more:
Advanced Grep Topics
GREP for Designers