plain text URL to HTML code (Automator/AppleScript)

橙三吉。 提交于 2019-12-24 15:30:35

问题


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.


回答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



回答2:


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


来源:https://stackoverflow.com/questions/13670756/plain-text-url-to-html-code-automator-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!