2

Using MacOS High Sierra, trying to using grep to extract the text between two "tags." Intended output should be "The Genre". Here's the .txt file:

<genre>
The Genre
</genre>

Here's the grep command that's not working:

genre=$(grep -o '(?<=<genre>).*?(=<\/genre>)' textfile.txt)

echo $genre

I am approaching this the wrong way for MacOS? All the commands I've seen with tags only support BNU or Linux variants. Help appreciated

nwood21
  • 312
  • 2
  • 13

1 Answers1

0

For multiline search you can use MacOS/FreeBSD pcregrep utility. It is installed with pcre package

pcregrep -o -M '(?s)(?<=<genre>).*(?=<\/genre>)' textfile.txt

Command explanation:

-o - show matched string

-M - use multiline search

(?s) - make "." to match any character whatsoever, even a newline

(?<=<genre>) - match prefix <genre> but not show it in output

(?=<\/genre>) - match postfix </genre> but not show it in output

Important note: This command will match everything between opening and closing tags including newlines. So output will be this:


The Genre

Alexander Ushakov
  • 5,139
  • 3
  • 27
  • 50