<a href="nyeeeeeeeeeeeeeee.txt">nyeeeeeeeeeeeeeee.txt</a>:sadfsadf
<a href="sadjflsajfd">sadjflsajfd</a>:jasdlfjalsfd
<a href="foo/asdfsafd.txt">foo/asdfsafd.txt</a>:laksjdlfajsfd

So I need to convert the INPUT to OUTPUT, the difference is that OUTPUT is "clickable" because this is in a HTML file.

Q: How can I do this with awk, sed or perl?

I know I can do a "read line by line", but what is the most resource efficient way to do this? (I only need 1 solution, not for all sed/awk/etc.) : is the delimiter.


Strictly what you asked for:

sed 's/\([^:]\+\)/<a href="\1">\1<\/a>/' input.txt
awk -F ':' -vOFS=':' '$1="<a href=\""$1"\">"$1"</a>"' input.txt
while IFS=':' read -r url text; do echo "<a href=\"$url\">$url</a>:$text"; done < input.txt

But for real HTML generating you also have to take care of HTML encoding:

perl -MHTML::Entities -pe 's!([^:]+)(.*)!"<a href=\"".($t=encode_entities($1))."\">$t".encode_entities($2)!e' input.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.