user3190544
user3190544

Reputation: 1

How to filter sting with "[" and "]" inside using grep?

How to filter anything inside "<torrent:magnetURI><![CDATA[" and "]]></torrent:magnetURI>" so it would output the string "EXAMPLE" using grep?

<torrent:magnetURI><![CDATA[EXAMPLE]]></torrent:magnetURI>

I'm trying to get all the magnet url in the web and add them to transmission.

for url in $(wget -q -O- "http://sample.com/rss.xml" | grep -o '<torrent:magnetURI><![CDATA["[^"]*' | grep -o '[^>]*$'); do 
transmission-remote localhost:9091 -a "$url"; 
done

Upvotes: 0

Views: 475

Answers (1)

fedorqui
fedorqui

Reputation: 290025

You can use:

$ grep -Po '(?<=<torrent:magnetURI><!\[CDATA\[)\w*(?=\]\]>)' file
EXAMPLE

Note it is using a look behind and look forward (?<=before)\w*(?=after), also escaping the [:

(?<=<torrent:magnetURI><!\[CDATA\[)\w*(?=\]\]>)
   ------------------------------- ---   -----
       string to find before        |    string after
                            string matched

Upvotes: 1

Related Questions