1

INPUT:

nyeeeeeeeeeeeeeee.txt:sadfsadf
sadjflsajfd:jasdlfjalsfd
foo/asdfsafd.txt:laksjdlfajsfd

OUTPUT:

<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.

1 Answer 1

5

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
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.