Tutorial :Remove links from text file



Question:

how can I remove links from a raw html text? I've got:

Foo bar <a href="http://www.foo.com">blah</a> bar foo   

and want to get:

Foo bar blah bar foo

afterwards.


Solution:1

You're looking to parse HTML with regexps, and this won't work in all but the simplest cases, since HTML isn't regular. A much more reliable solution is to use an HTML parser. Numerous exist, for many different languages.


Solution:2

sed -re 's|<a [^>]*>([^<]*)</a>|\1|g'  

But Brian's answer is right: This should only be used in very simple cases.


Solution:3

try with:

sed -e 's/<a[^>]*>.*<\/a>//g' test.txt  


Solution:4

$ echo 'Foo bar <a href="http://www.foo.com">blah</a> bar foo' | awk 'BEGIN{RS="</a>"}/<a href/{gsub(/<a href=\042.*\042>/,"")}1'

Foo bar blah bar foo


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »