问题
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