1

Suppose I have a plain txt file in a text editor such as TextEdit:

title 1
http://a.b/c

title 2
http://d.e/f

...

I'd like to convert all the lines beginning with http:// to HTML code for URL, so that the aforementioned content will become:

title 1
<a href="http://a.b/c">http://a.b/c</a>

title 2
<a href="http://d.e/f">http://d.e/f</a>

...

How can I get this done in Automator or AppleScript? (My current solution is using Gmail, but it involves multi-step copy-paste.)

Thank you very much in advance.

2 Answers 2

1

This will let you avoid another editor:

set inFile to "/Users/you/Desktop/Urls.txt"
set outFile to "/Users/you/Desktop/Urls2.txt"

do shell script "sed 's/\\(http[^ ]*\\)/<a href=\"\\1\">\\1<\\/a>/g' " & quoted form of inFile & " >" & quoted form of outFile
Sign up to request clarification or add additional context in comments.

Comments

0

Just do a regex search and replace in a text editor or Terminal:

sed -E 's|^(http:.*)|<a href="\1">\1</a>|g' file.txt

1 Comment

Thank you very much for the quick and helpful response! Since the Terminal method only replaced the first URL, I looked for a Mac application that supported regex expression. I got MacVim. There is a "Convert to HTML" menu command in the "Syntax" menu of MacVim that accomplishes the replacement I wanted perfectly. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.